Skip to content

Commit bf721e1

Browse files
committed
code cleanup
cleaned the code up, put all the commun fonctions between builder and updater under a single file and have them shared trough a namespaces
1 parent 542c4de commit bf721e1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+9940
-5962
lines changed
Lines changed: 374 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,374 @@
1+
using UnityEngine;
2+
using UnityEditor;
3+
using System;
4+
using System.IO;
5+
using System.Text.RegularExpressions;
6+
using System.Diagnostics;
7+
using System.Collections;
8+
9+
10+
namespace EditDistributor
11+
{
12+
13+
public class CommunFonctions : MonoBehaviour
14+
{
15+
16+
17+
public static string GetRelativePath(string fromPath, string toPath)
18+
{
19+
if (string.IsNullOrEmpty(fromPath) || string.IsNullOrEmpty(toPath))
20+
{
21+
throw new ArgumentException("Both paths must be non-empty");
22+
}
23+
24+
Uri fromUri = new Uri(fromPath);
25+
Uri toUri = new Uri(toPath);
26+
27+
if (fromUri.Scheme != toUri.Scheme)
28+
{
29+
// The paths have different schemes (e.g., file and http), so we can't construct a relative path.
30+
return toPath;
31+
}
32+
33+
Uri relativeUri = fromUri.MakeRelativeUri(toUri);
34+
return Uri.UnescapeDataString(relativeUri.ToString());
35+
}
36+
37+
38+
39+
public static void AddCenteredLabel(string mymessage)
40+
{
41+
EditorGUILayout.BeginHorizontal();
42+
GUILayout.FlexibleSpace();
43+
EditorGUILayout.LabelField(mymessage);
44+
GUILayout.FlexibleSpace();
45+
EditorGUILayout.EndHorizontal();
46+
}
47+
48+
49+
public static void AddBoldCenteredLabel(string mymessage)
50+
{
51+
GUIStyle boldLabelStyle = new GUIStyle(EditorStyles.boldLabel);
52+
EditorGUILayout.BeginHorizontal();
53+
GUILayout.FlexibleSpace();
54+
EditorGUILayout.LabelField(mymessage, EditorStyles.boldLabel);
55+
GUILayout.FlexibleSpace();
56+
EditorGUILayout.EndHorizontal();
57+
}
58+
59+
60+
61+
public static void CheckAndCopyFileIfExists(string OGPath, string DestPath)
62+
{
63+
if (File.Exists(OGPath))
64+
{
65+
File.Copy(OGPath, DestPath);
66+
}
67+
else
68+
{
69+
PrintMissingFileError(OGPath);
70+
}
71+
}
72+
73+
74+
75+
public static void PrintMissingFileError(string PathToFile)
76+
{
77+
UnityEngine.Debug.Log("please make sure you have " + Path.GetFileName(PathToFile) + " in: " + PathToFile);
78+
}
79+
80+
81+
82+
public static void DeleteAllFilesInDirectory(string directoryPath)
83+
{
84+
try
85+
{
86+
// Check if the directory exists
87+
if (Directory.Exists(directoryPath))
88+
{
89+
// Get all files in the directory
90+
string[] files = Directory.GetFiles(directoryPath);
91+
92+
// Delete each file
93+
foreach (string filePath in files)
94+
{
95+
File.Delete(filePath);
96+
UnityEngine.Debug.Log($"File deleted: {filePath}");
97+
}
98+
99+
UnityEngine.Debug.Log("Deletion process completed successfully.");
100+
}
101+
else
102+
{
103+
UnityEngine.Debug.Log("Directory does not exist.");
104+
}
105+
}
106+
catch (Exception ex)
107+
{
108+
UnityEngine.Debug.Log($"An error occurred: {ex.Message}");
109+
}
110+
}
111+
112+
113+
114+
115+
public static void ReplaceStringsAndCopyFiles(string sourceDirectory, string targetDirectory, string[] searchPatterns, string[] replacementValues)
116+
{
117+
try
118+
{
119+
// Check if the source directory exists
120+
if (Directory.Exists(sourceDirectory))
121+
{
122+
// Create the target directory if it doesn't exist
123+
if (!Directory.Exists(targetDirectory))
124+
{
125+
Directory.CreateDirectory(targetDirectory);
126+
}
127+
128+
// Iterate through all directories and subdirectories
129+
string[] allDirectories = Directory.GetDirectories(sourceDirectory, "*", SearchOption.AllDirectories);
130+
131+
foreach (string currentDirectory in allDirectories)
132+
{
133+
// Get all files in the current directory
134+
string[] files = Directory.GetFiles(currentDirectory);
135+
136+
// Iterate over each file, replace strings, and copy to the target directory
137+
foreach (string filePath in files)
138+
{
139+
// Read the content of the file
140+
string content = File.ReadAllText(filePath);
141+
142+
// Replace strings in the content
143+
for (int i = 0; i < searchPatterns.Length; i++)
144+
{
145+
content = content.Replace(searchPatterns[i], replacementValues[i]);
146+
}
147+
148+
// Get the relative path within the source directory
149+
string relativePath = filePath.Substring(sourceDirectory.Length + 1);
150+
151+
// Create the target path by combining the target directory and the relative path
152+
string targetFilePath = Path.Combine(targetDirectory, relativePath);
153+
154+
// Create the target directory for the file if it doesn't exist
155+
string targetFileDirectory = Path.GetDirectoryName(targetFilePath);
156+
if (!Directory.Exists(targetFileDirectory))
157+
{
158+
Directory.CreateDirectory(targetFileDirectory);
159+
}
160+
161+
// Write the modified content to the target file
162+
File.WriteAllText(targetFilePath, content);
163+
164+
// Log using UnityEngine.Debug.Log instead of Console.WriteLine
165+
UnityEngine.Debug.Log($"File copied and strings replaced: {targetFilePath}");
166+
}
167+
}
168+
169+
// Copy root directory files as well
170+
string[] rootFiles = Directory.GetFiles(sourceDirectory);
171+
foreach (string filePath in rootFiles)
172+
{
173+
// Read the content of the file
174+
string content = File.ReadAllText(filePath);
175+
176+
// Replace strings in the content
177+
for (int i = 0; i < searchPatterns.Length; i++)
178+
{
179+
content = content.Replace(searchPatterns[i], replacementValues[i]);
180+
}
181+
182+
// Get the relative path within the source directory
183+
string relativePath = filePath.Substring(sourceDirectory.Length + 1);
184+
185+
// Create the target path by combining the target directory and the relative path
186+
string targetFilePath = Path.Combine(targetDirectory, relativePath);
187+
188+
// Create the target directory for the file if it doesn't exist
189+
string targetFileDirectory = Path.GetDirectoryName(targetFilePath);
190+
if (!Directory.Exists(targetFileDirectory))
191+
{
192+
Directory.CreateDirectory(targetFileDirectory);
193+
}
194+
195+
// Write the modified content to the target file
196+
File.WriteAllText(targetFilePath, content);
197+
198+
// Log using UnityEngine.Debug.Log instead of Console.WriteLine
199+
UnityEngine.Debug.Log($"Root file copied and strings replaced: {targetFilePath}");
200+
}
201+
202+
UnityEngine.Debug.Log("Replacement and copying process completed successfully.");
203+
}
204+
else
205+
{
206+
UnityEngine.Debug.Log("Source directory does not exist.");
207+
}
208+
}
209+
catch (Exception ex)
210+
{
211+
UnityEngine.Debug.LogError($"An error occurred: {ex.Message}");
212+
}
213+
}
214+
215+
216+
217+
218+
219+
220+
public static void ReplaceStringsInFile(string filePath, string[] searchPatterns, string[] replacementValues)
221+
{
222+
if (searchPatterns.Length != replacementValues.Length)
223+
{
224+
throw new ArgumentException("Search patterns and replacement values arrays must have the same length.");
225+
}
226+
227+
string content = File.ReadAllText(filePath);
228+
229+
for (int i = 0; i < searchPatterns.Length; i++)
230+
{
231+
string escapedPattern = Regex.Escape(searchPatterns[i]);
232+
content = Regex.Replace(content, escapedPattern, replacementValues[i]);
233+
}
234+
235+
File.WriteAllText(filePath, content);
236+
}
237+
238+
239+
240+
public static bool DirectoryContainsExe(string directoryPath)
241+
{
242+
try
243+
{
244+
// Check if the directory exists
245+
if (Directory.Exists(directoryPath))
246+
{
247+
// Get all files in the directory
248+
string[] files = Directory.GetFiles(directoryPath);
249+
250+
// Check if any file ends with ".exe"
251+
foreach (string filePath in files)
252+
{
253+
if (Path.GetExtension(filePath).Equals(".exe", StringComparison.OrdinalIgnoreCase))
254+
{
255+
return true;
256+
}
257+
}
258+
}
259+
}
260+
catch (Exception ex)
261+
{
262+
Console.WriteLine($"An error occurred: {ex.Message}");
263+
}
264+
265+
// Return false if no .exe files are found or an error occurs
266+
return false;
267+
}
268+
269+
270+
public static string FindFbxPathInAnimator(GameObject prefab)
271+
{
272+
273+
if (prefab != null)
274+
{
275+
Animator animator = prefab.GetComponent<Animator>();
276+
277+
if (animator != null && animator.avatar != null)
278+
{
279+
Avatar avatar = animator.avatar;
280+
281+
return GetFBXPathFromAvatar(avatar);
282+
}
283+
}
284+
285+
return string.Empty;
286+
}
287+
288+
289+
private static string GetFBXPathFromAvatar(Avatar avatar)
290+
{
291+
string avatarGUID = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(avatar));
292+
string[] assetPaths = AssetDatabase.GetAllAssetPaths();
293+
294+
foreach (string assetPath in assetPaths)
295+
{
296+
if (AssetDatabase.GUIDToAssetPath(avatarGUID) == assetPath && Path.GetExtension(assetPath).ToLower() == ".fbx")
297+
{
298+
string dataPath = Application.dataPath;
299+
string relativePath = assetPath.Substring("Assets/".Length);
300+
string fullPath = Path.Combine(dataPath, relativePath);
301+
fullPath = Path.GetFullPath(fullPath);
302+
return fullPath;
303+
}
304+
}
305+
306+
return string.Empty;
307+
}
308+
309+
310+
public static string GoUpNDirs(string MyDir, int n)
311+
{
312+
MyDir = Path.GetFullPath(MyDir);
313+
string[] pathComponents = MyDir.Split('\\');
314+
if (pathComponents.Length >= n)
315+
{
316+
317+
string parentPath = string.Join("\\", pathComponents, 0, pathComponents.Length - n);
318+
return Path.Combine(parentPath);
319+
}
320+
else return string.Empty;
321+
322+
}
323+
324+
public static string GetRelativePathToAssets(string fullPath)
325+
{
326+
string assetsFolder = "Assets";
327+
int assetsIndex = fullPath.IndexOf(assetsFolder, StringComparison.OrdinalIgnoreCase);
328+
329+
if (assetsIndex != -1)
330+
{
331+
string relativePath = fullPath.Substring(assetsIndex + assetsFolder.Length + 1);
332+
333+
return relativePath.Replace(Path.DirectorySeparatorChar, '/');
334+
}
335+
336+
return fullPath;
337+
}
338+
339+
340+
public static void LaunchProgramWithArguments(string programPath, string arguments)
341+
{
342+
try
343+
{
344+
var process = new Process
345+
{
346+
StartInfo = new ProcessStartInfo()
347+
{
348+
FileName = programPath,
349+
Arguments = arguments,
350+
WorkingDirectory = Application.dataPath,
351+
UseShellExecute = false,
352+
RedirectStandardError = true,
353+
RedirectStandardInput = true,
354+
RedirectStandardOutput = true,
355+
CreateNoWindow = true,
356+
}
357+
358+
};
359+
360+
361+
process.Start();
362+
UnityEngine.Debug.Log(process.StandardOutput.ReadToEnd());
363+
process.WaitForExit();
364+
365+
}
366+
catch (Exception e)
367+
{
368+
UnityEngine.Debug.Log(e.Message);
369+
}
370+
}
371+
372+
}
373+
374+
}

DemoProject/Assets/Hash's_Things/EditDistributor/Editor/CommunFonctions.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)