|
4 | 4 | using System.Collections.Generic; |
5 | 5 | using System.Diagnostics; |
6 | 6 | using System.IO; |
| 7 | +using System.Linq; |
| 8 | +using System.Runtime.CompilerServices; |
7 | 9 | using System.Security.Cryptography; |
8 | 10 | using System.Text; |
9 | 11 | #if UNITY_EDITOR || UNITY_STANDALONE |
10 | 12 | using UnityEngine; |
11 | 13 | #endif |
12 | 14 |
|
| 15 | +[assembly: InternalsVisibleTo("CATHODE.Scripting")] |
13 | 16 | namespace CATHODE.Scripting |
14 | 17 | { |
15 | 18 | public static class ShortGuidUtils |
16 | 19 | { |
17 | 20 | private static GuidNameTable _custom = new GuidNameTable(); |
18 | 21 |
|
19 | | - public static Commands LinkedCommands => _commands; |
20 | | - private static Commands _commands; |
21 | | - |
22 | | - /* Optionally, link a Commands file which can be used to save custom ShortGuids to */ |
23 | | - public static void LinkCommands(Commands commands) |
24 | | - { |
25 | | - if (_commands != null) |
26 | | - _commands.OnSaveSuccess -= SaveCustomNames; |
27 | | - |
28 | | - _commands = commands; |
29 | | - if (_commands == null) return; |
30 | | - |
31 | | - _commands.OnSaveSuccess += SaveCustomNames; |
32 | | - |
33 | | - LoadCustomNames(commands.Filepath); |
34 | | - } |
35 | | - |
36 | 22 | /* Generate a ShortGuid to interface with the Cathode scripting system */ |
37 | 23 | public static ShortGuid Generate(string value, bool cache = true) |
38 | 24 | { |
@@ -86,31 +72,60 @@ public static ShortGuid GenerateRandom() |
86 | 72 | } |
87 | 73 |
|
88 | 74 | /* Cache a pre-generated ShortGuid */ |
89 | | - private static void Cache(ShortGuid guid, string value) |
| 75 | + private static bool Cache(ShortGuid guid, string value) |
90 | 76 | { |
91 | | - //TODO: need to fix this for BSPNOSTROMO_RIPLEY_PATCH (?) |
92 | | - if (_custom.cache.ContainsKey(value)) return; |
| 77 | + if (_custom.cache.ContainsKey(value)) return false; |
93 | 78 | _custom.cache.Add(value, guid); |
94 | 79 | try |
95 | 80 | { |
| 81 | + //TODO: need to fix this for BSPNOSTROMO_RIPLEY_PATCH (?) |
96 | 82 | _custom.cacheReversed.Add(guid, value); |
97 | 83 | } |
98 | 84 | catch { } |
| 85 | + return true; |
99 | 86 | } |
100 | 87 |
|
101 | | - /* Pull non-vanilla ShortGuid from the CommandsPAK */ |
102 | | - private static void LoadCustomNames(string filepath) |
| 88 | + #region Commands Linking |
| 89 | + private static List<Commands> _commands = new List<Commands>(); |
| 90 | + |
| 91 | + /* Utilised by CommandsUtils: this connects up a loaded Commands object, allowing generated ShortGuids to be stored for re-use next session */ |
| 92 | + internal static void LinkCommands(Commands commands) |
103 | 93 | { |
104 | | - _custom = (GuidNameTable)CustomTable.ReadTable(filepath, CustomTableType.SHORT_GUIDS); |
105 | | - if (_custom == null) _custom = new GuidNameTable(); |
106 | | - Console.WriteLine("Loaded " + _custom.cache.Count + " custom ShortGuids!"); |
| 94 | + if (_commands.FirstOrDefault(o => o.Filepath == commands.Filepath) != null) |
| 95 | + return; |
| 96 | + |
| 97 | + _commands.Add(commands); |
| 98 | + commands.OnSaveSuccess += SaveCustomNames; |
| 99 | + LoadCustomNames(commands.Filepath); |
107 | 100 | } |
| 101 | + internal static void UnlinkCommands(Commands commands) |
| 102 | + { |
| 103 | + Commands linkedCommands = _commands.FirstOrDefault(o => o.Filepath == commands.Filepath); |
| 104 | + if (linkedCommands == null) |
| 105 | + return; |
| 106 | + _commands.Remove(linkedCommands); |
| 107 | + } |
| 108 | + |
| 109 | + /* Load/save custom shortguids */ |
| 110 | + private static void LoadCustomNames(string filepath) |
| 111 | + { |
| 112 | + GuidNameTable guids = (GuidNameTable)CustomTable.ReadTable(filepath, CustomTableType.SHORT_GUIDS); |
| 113 | + if (guids == null) |
| 114 | + return; |
108 | 115 |
|
109 | | - /* Write non-vanilla entity names to the CommandsPAK */ |
| 116 | + int added = 0; |
| 117 | + foreach (KeyValuePair<string, ShortGuid> str in guids.cache) |
| 118 | + { |
| 119 | + if (Cache(str.Value, str.Key)) |
| 120 | + added++; |
| 121 | + } |
| 122 | + Console.WriteLine("Loaded " + added + " ShortGuids!"); |
| 123 | + } |
110 | 124 | private static void SaveCustomNames(string filepath) |
111 | 125 | { |
112 | 126 | CustomTable.WriteTable(filepath, CustomTableType.SHORT_GUIDS, _custom); |
113 | | - Console.WriteLine("Saved " + _custom.cache.Count + " custom ShortGuids!"); |
| 127 | + Console.WriteLine("Saved " + _custom.cache.Count + " ShortGuids!"); |
114 | 128 | } |
| 129 | + #endregion |
115 | 130 | } |
116 | 131 | } |
0 commit comments