Repository navigation
Expand file tree
/
Copy pathAddHeaderToScripts.cs
More file actions
151 lines (125 loc) · 6.24 KB
/
Copy pathAddHeaderToScripts.cs
File metadata and controls
151 lines (125 loc) · 6.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using UnityEngine;
using UnityEditor;
using System.IO;
using System;
using System.Collections;
using System.Collections.Generic;
public class AddNamespaceToScriptWindow : EditorWindow
{
public PlayerData player;
[MenuItem("Custom Tools/Add Namespace to Specific Folder", false, 0)]
public static void AddNamespaceFolder()
{
//string relativeFilePath = "Fuly UnlockGame/Scripts/BearScripts.cs"; // Relative to "Assets" folder
//string absoluteFilePath = Path.Combine(Application.dataPath, relativeFilePath);
string namespaceText = "namespace SlimeGirlsFriend {\n";
string endNamespaceText = "}\n";
//AddNamespaceToScript(absoluteFilePath, namespaceText, endNamespaceText);
string folderPath = "Assets/SlimeGirlfriend/" /*"Assets/Fuly UnlockGame/TransparentCapture"*/ ; // Change "YourFolderName" to the actual folder name
string[] scriptFiles = Directory.GetFiles(folderPath, "*.cs", SearchOption.AllDirectories);
foreach (string filePath in scriptFiles)
{
AddNamespaceToScript(filePath, namespaceText, endNamespaceText);
}
Debug.Log("Namespace added to the specified script.");
}
//[MenuItem("Custom Tools/Add Namespace to Specific Script", false, 0)]
//public static void AddNameSpaceToScript()
// {
// string relativeFilePath = "NameSpace/NewBehaviourScript"; /*"Fuly UnlockGame/Scripts/.cs";*/ // Relative to "Assets" folder
// string absoluteFilePath = Path.Combine(Application.dataPath, relativeFilePath);
// //AddNamespaceToScript(absoluteFilePath, namespaceText, endNamespaceText);
// string namespaceText = "namespace SlimeGirlsFriend {\n";
// string endNamespaceText = "}\n";
// AddNamespaceToScript(absoluteFilePath, namespaceText, endNamespaceText);
// }
static void AddNamespaceToScript(string filePath, string namespaceText, string endNamespaceText)
{
// Check if the specified script file exists
if (File.Exists(filePath))
{
string originalContent = File.ReadAllText(filePath);
if (originalContent.Contains("namespace SlimeGirlsFriend"))
{
Debug.Log("namespace SlimeGirlsFriend" + "Already Exsists");
// i want to rplace the namespace with new one can you modify the code in this methood if block
return;
}
// Check if the script already contains the specified namespace
if (originalContent.Contains("namespace"))
{
string text = "namespace";
if (originalContent.Contains(text))
{
string classDefinition = GetClassDefinition(originalContent, text);
string headerFiles = GetHeaderFiles(originalContent,text);
string restOfFile = GetRestOfFile(originalContent, classDefinition);
string newContent = headerFiles + namespaceText + classDefinition + restOfFile + endNamespaceText;
File.WriteAllText(filePath, newContent);
Debug.Log("Added before NameSpace");
return;
}
}
else
if (originalContent.Contains("[System.Serializable]"))
{
string text = "[System.Serializable]";
if (originalContent.Contains(text))
{
string classDefinition = GetClassDefinition(originalContent, text);
string headerFiles = GetHeaderFiles(originalContent, text);
string restOfFile = GetRestOfFile(originalContent, classDefinition);
string newContent = headerFiles + namespaceText + classDefinition + restOfFile + endNamespaceText;
File.WriteAllText(filePath, newContent);
Debug.Log("Added before [System.Serializable]");
return;
}
}
else
// Check if the script contains a class definition
if (originalContent.Contains("public class"))
{
// Add the namespace around the class definition
string text = "public class";
string classDefinition = GetClassDefinition(originalContent, text);
string headerFiles = GetHeaderFiles(originalContent, text);
string restOfFile = GetRestOfFile(originalContent, classDefinition);
string newContent = headerFiles + namespaceText + classDefinition + restOfFile + endNamespaceText;
File.WriteAllText(filePath, newContent);
Debug.Log("Added before class");
return;
}
else
{
Debug.LogError("Specified script does not contain a class definition.");
}
}
else
{
Debug.Log("Namespace already exists in the specified script." + filePath);
}
}
static string GetClassDefinition(string scriptContent,string text)
{
// Extract the class definition
int classStartIndex = scriptContent.IndexOf(text);
int classEndIndex = scriptContent.IndexOf("{", classStartIndex);
if (classStartIndex != -1 && classEndIndex != -1)
{
return scriptContent.Substring(classStartIndex, classEndIndex - classStartIndex);
}
return string.Empty;
}
static string GetHeaderFiles(string scriptContent, string text)
{
// Extract the content before the class definition
int classStartIndex = scriptContent.IndexOf(text);
return scriptContent.Substring(0, classStartIndex);
}
static string GetRestOfFile(string scriptContent, string classDefinition)
{
// Extract the content after the class definition
int classEndIndex = scriptContent.IndexOf(classDefinition) + classDefinition.Length;
return scriptContent.Substring(classEndIndex);
}
}