-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathScriptPostprocessor.cs
More file actions
91 lines (83 loc) · 3.24 KB
/
Copy pathScriptPostprocessor.cs
File metadata and controls
91 lines (83 loc) · 3.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
#if UNITY_EDITOR
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using System.IO;
using System;
using System.Linq;
namespace E7.E7Unity.ScriptPostprocessor
{
public class ScriptPostprocessor : AssetPostprocessor
{
private static bool anyChanges = false;
static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
{
anyChanges = false;
foreach (string str in importedAssets)
{
//Debug.Log($"Importing {str}");
if (Postprocess(str) == true)
{
anyChanges = true;
}
}
if (anyChanges)
{
Debug.Log(nameof(ScriptPostprocessor));
AssetDatabase.Refresh();
}
}
private static string postprocessedString = "//--Script Postprocessed--";
private static bool Postprocess(string path)
{
string[] find = AssetDatabase.FindAssets("ScriptPostprocessor t:ScriptableObject");
if (find.Length == 0)
{
return false;
}
//Debug.Log($"Found config file {AssetDatabase.GUIDToAssetPath(find[0])}");
ScriptPostprocessorConfig sps = AssetDatabase.LoadAssetAtPath<ScriptPostprocessorConfig>(AssetDatabase.GUIDToAssetPath(find[0]));
bool changes = false;
foreach (ScriptPostprocessorOrder spo in sps.Orders)
{
if (spo.fileName == Path.GetFileName(path))
{
bool foundLine = false;
List<string> lines = File.ReadAllLines(path).ToList();
List<string> modifiedLines = new List<string>();
if(lines.Count > 0 && lines[0] == postprocessedString)
{
//Debug.Log($"[ScriptPostprocessor] {spo.fileName} already postprocessed.");
continue;
}
Debug.Log($"[ScriptPostprocessor] Detected changes of {spo.fileName}.");
foreach (string line in lines)
{
foreach (ScriptPostprocessorLine spl in spo.lines)
{
if (line.Trim() == spl.line.Trim())
{
foreach (string add in spl.addToAbove)
{
Debug.Log($"[ScriptPostprocessor] Adding {add} above {line} in {spo.fileName}");
modifiedLines.Add(add);
foundLine = true;
}
}
}
modifiedLines.Add(line);
}
if (foundLine)
{
modifiedLines.Insert(0, postprocessedString);
File.WriteAllLines(path, modifiedLines);
changes = true;
}
}
}
return changes;
}
}
}
#endif