| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Diagnostics; |
| | 4 | | using System.IO; |
| | 5 | | using System.Linq; |
| | 6 | | using System.Net; |
| | 7 | | using System.Text; |
| | 8 | | using System.Text.RegularExpressions; |
| | 9 | | using UnityEditor; |
| | 10 | | using UnityEngine; |
| | 11 | | using Debug = UnityEngine.Debug; |
| | 12 | |
|
| | 13 | | using Newtonsoft.Json; |
| | 14 | |
|
| | 15 | | using System.IO.Compression; |
| | 16 | | using UnityEditor.Compilation; |
| | 17 | | using ICSharpCode.SharpZipLib.GZip; |
| | 18 | | using ICSharpCode.SharpZipLib.Tar; |
| | 19 | | using ICSharpCode.SharpZipLib.Zip; |
| | 20 | |
|
| | 21 | | namespace DCL.Protobuf |
| | 22 | | { |
| | 23 | | [InitializeOnLoad] |
| | 24 | | public static class ProtobufEditor |
| | 25 | | { |
| | 26 | | static ProtobufEditor() |
| | 27 | | { |
| 0 | 28 | | CompilationPipeline.compilationStarted += OnProjectCompile; |
| 0 | 29 | | OnProjectCompile(); |
| 0 | 30 | | } |
| | 31 | |
|
| | 32 | | private const bool VERBOSE = true; |
| | 33 | |
|
| | 34 | | private const string PATH_TO_GENERATED = "/DCLPlugins/ECS7/ProtocolBuffers/Generated/"; |
| | 35 | | private const string REALPATH_TO_COMPONENTS_DEFINITIONS = "/DCLPlugins/ECS7/ProtocolBuffers/Definitions"; |
| | 36 | | private const string PATH_TO_COMPONENTS = "/DCLPlugins/ECS7/ProtocolBuffers/Generated/PBFiles"; |
| | 37 | | private const string SUBPATH_TO_COMPONENTS_COMMON = "/Common"; |
| | 38 | | private const string TEMPPATH_TO_COMPONENTS_DEFINITIONS = "/DCLPlugins/ECS7/ProtocolBuffers/DefinitionsTemp"; |
| | 39 | | private const string PATHNAME_TO_COMPONENTS_DEFINITIONS_COMMON = "common"; |
| | 40 | | private const string PATH_TO_COMPONENT_IDS = "/DCLPlugins/ECS7/ProtocolBuffers/Generated/ComponentID/ComponentID |
| | 41 | | private const string PATH_TO_FOLDER = "/DCLPlugins/ECS7/ProtocolBuffers/Editor/"; |
| | 42 | | private const string PATH_TO_PROTO = "/DCLPlugins/ECS7/ProtocolBuffers/Editor/bin/"; |
| | 43 | |
|
| | 44 | | private const string PROTO_FILENAME = "protoc"; |
| | 45 | | private const string DOWNLOADED_VERSION_FILENAME = "downloadedVersion.gen.txt"; |
| | 46 | | private const string COMPILED_VERSION_FILENAME = "compiledVersion.gen.txt"; |
| | 47 | | private const string EXECUTABLE_VERSION_FILENAME = "executableVersion.gen.txt"; |
| | 48 | |
|
| | 49 | | private const string PROTO_VERSION = "3.20.1"; |
| | 50 | |
|
| | 51 | | // Use this parameter when you want a fixed version of the @dcl/protocol, otherwise leave it empty |
| | 52 | | //private const string FIXED_NPM_PACKAGE_LINK = "https://sdk-team-cdn.decentraland.org/@dcl/protocol/branch//dcl |
| | 53 | | private const string FIXED_NPM_PACKAGE_LINK = ""; |
| | 54 | |
|
| | 55 | | private const string NPM_PACKAGE = "@dcl/protocol"; |
| | 56 | | private const string NPM_PACKAGE_PROTO_DEF = "/package/ecs/components/"; |
| | 57 | |
|
| | 58 | | private struct ProtoComponent |
| | 59 | | { |
| | 60 | | public string componentName; |
| | 61 | | public int componentId; |
| | 62 | | } |
| | 63 | |
|
| | 64 | | private static void VerboseLog(string message) |
| | 65 | | { |
| | 66 | | if (VERBOSE) |
| | 67 | | { |
| 0 | 68 | | Debug.Log(message); |
| | 69 | | } |
| 0 | 70 | | } |
| | 71 | |
|
| | 72 | | [MenuItem("Decentraland/Protobuf/UpdateModels with the latest version")] |
| | 73 | | public static void UpdateModels() |
| | 74 | | { |
| 0 | 75 | | var lastVersion = GetLatestProtoVersion(); |
| 0 | 76 | | UpdateModels(lastVersion); |
| 0 | 77 | | } |
| | 78 | |
|
| | 79 | | public static void UpdateModels(string version) |
| | 80 | | { |
| 0 | 81 | | if (!IsProtoVersionValid()) |
| 0 | 82 | | DownloadProtobuffExecutable(); |
| | 83 | |
|
| 0 | 84 | | DownloadProtoDefinitions(version); |
| 0 | 85 | | GenerateComponentCode(version); |
| 0 | 86 | | CompilationPipeline.RequestScriptCompilation(); |
| 0 | 87 | | } |
| | 88 | |
|
| | 89 | | [MenuItem("Decentraland/Protobuf/Download latest proto definitions (For debugging)")] |
| | 90 | | public static void DownloadLatestProtoDefinitions() |
| | 91 | | { |
| 0 | 92 | | var nextVersion = GetLatestProtoVersion(); |
| 0 | 93 | | DownloadProtoDefinitions(nextVersion); |
| 0 | 94 | | } |
| | 95 | |
|
| | 96 | | public static void DownloadProtoDefinitions(string version) |
| | 97 | | { |
| | 98 | | WebClient client; |
| | 99 | | Stream data; |
| | 100 | | StreamReader reader; |
| | 101 | | string libraryJsonString; |
| | 102 | | Dictionary<string, object> libraryContent, libraryInfo; |
| | 103 | |
|
| 0 | 104 | | VerboseLog("Downloading " + NPM_PACKAGE + " version: " + version); |
| | 105 | |
|
| | 106 | | // Download the "package.json" of {NPM_PACKAGE}@version |
| 0 | 107 | | client = new WebClient(); |
| 0 | 108 | | data = client.OpenRead(@"https://registry.npmjs.org/" + NPM_PACKAGE + "/" + version); |
| 0 | 109 | | reader = new StreamReader(data); |
| 0 | 110 | | libraryJsonString = reader.ReadToEnd(); |
| 0 | 111 | | data.Close(); |
| 0 | 112 | | reader.Close(); |
| | 113 | |
|
| | 114 | | // Process the response |
| 0 | 115 | | libraryContent = JsonConvert.DeserializeObject<Dictionary<string, object>>(libraryJsonString); |
| 0 | 116 | | libraryInfo = JsonConvert.DeserializeObject<Dictionary<string, object>>(libraryContent["dist"].ToString()); |
| | 117 | |
|
| 0 | 118 | | string tgzUrl = libraryInfo["tarball"].ToString(); |
| | 119 | |
|
| | 120 | | // If we have a fixed version, use it |
| 0 | 121 | | if (FIXED_NPM_PACKAGE_LINK.Length > 0) { |
| 0 | 122 | | tgzUrl = FIXED_NPM_PACKAGE_LINK; |
| | 123 | | } |
| | 124 | |
|
| 0 | 125 | | VerboseLog(NPM_PACKAGE + "@" + version + "url: " + tgzUrl); |
| | 126 | |
|
| | 127 | | // Download package |
| 0 | 128 | | string packageWithoutSlash = NPM_PACKAGE.Replace("/", "-"); // Replace / because in the file system is inter |
| 0 | 129 | | string packageName = packageWithoutSlash + "-" + version + ".tgz"; |
| 0 | 130 | | client = new WebClient(); |
| 0 | 131 | | client.DownloadFile(tgzUrl, packageName); |
| 0 | 132 | | VerboseLog("File downloaded " + packageName); |
| | 133 | |
|
| 0 | 134 | | string destPackage = packageWithoutSlash + "-" + version; |
| 0 | 135 | | if (Directory.Exists(destPackage)) |
| 0 | 136 | | Directory.Delete(destPackage, true); |
| | 137 | |
|
| | 138 | | try |
| | 139 | | { |
| 0 | 140 | | Directory.CreateDirectory(destPackage); |
| | 141 | |
|
| 0 | 142 | | Untar(packageName,destPackage); |
| 0 | 143 | | VerboseLog("Untar " + packageName); |
| | 144 | |
|
| 0 | 145 | | if (File.Exists(destPackage + NPM_PACKAGE_PROTO_DEF + "/" + PATHNAME_TO_COMPONENTS_DEFINITIONS_COMMON + |
| | 146 | | { |
| 0 | 147 | | File.Delete(destPackage + NPM_PACKAGE_PROTO_DEF + "/" + PATHNAME_TO_COMPONENTS_DEFINITIONS_COMMON + |
| | 148 | | } |
| | 149 | |
|
| 0 | 150 | | string componentDefinitionPath = Application.dataPath + REALPATH_TO_COMPONENTS_DEFINITIONS; |
| | 151 | |
|
| 0 | 152 | | if (Directory.Exists(componentDefinitionPath)) |
| 0 | 153 | | Directory.Delete(componentDefinitionPath, true); |
| | 154 | |
|
| | 155 | | // We move the definitions to their correct path |
| 0 | 156 | | Directory.Move(destPackage + NPM_PACKAGE_PROTO_DEF, componentDefinitionPath); |
| | 157 | |
|
| 0 | 158 | | VerboseLog("Success copying definitions in " + componentDefinitionPath); |
| | 159 | |
|
| 0 | 160 | | } |
| 0 | 161 | | catch (Exception e) |
| | 162 | | { |
| 0 | 163 | | Debug.LogError("The download has failed " + e.Message); |
| 0 | 164 | | } |
| | 165 | | finally // We delete the downloaded package |
| | 166 | | { |
| 0 | 167 | | Directory.Delete(destPackage, true); |
| 0 | 168 | | if (File.Exists(packageName)) |
| 0 | 169 | | File.Delete(packageName); |
| 0 | 170 | | } |
| 0 | 171 | | } |
| | 172 | |
|
| | 173 | | private static List<ProtoComponent> GetComponents() |
| | 174 | | { |
| | 175 | | // We get all the files that are proto |
| 0 | 176 | | DirectoryInfo dir = new DirectoryInfo(Application.dataPath + REALPATH_TO_COMPONENTS_DEFINITIONS); |
| 0 | 177 | | FileInfo[] info = dir.GetFiles("*.proto"); |
| 0 | 178 | | List<ProtoComponent> components = new List<ProtoComponent>(); |
| | 179 | |
|
| 0 | 180 | | foreach (FileInfo file in info) |
| | 181 | | { |
| | 182 | | // We ensure that only proto files are converted, this shouldn't be necessary but just in case |
| 0 | 183 | | if (!file.Name.Contains(".proto")) |
| | 184 | | continue; |
| | 185 | |
|
| 0 | 186 | | string protoContent = File.ReadAllText(file.FullName); |
| | 187 | |
|
| 0 | 188 | | ProtoComponent component = new ProtoComponent(); |
| 0 | 189 | | component.componentName = file.Name.Substring(0, file.Name.Length - 6); |
| 0 | 190 | | component.componentId = -1; |
| | 191 | |
|
| 0 | 192 | | Regex regex = new Regex(@" *option +\(ecs_component_id\) += +[0-9]+ *;"); |
| 0 | 193 | | var result = regex.Match(protoContent); |
| 0 | 194 | | if (result.Length > 0) |
| | 195 | | { |
| 0 | 196 | | string componentIdStr = result.Value.Split('=')[1].Split(';')[0]; |
| 0 | 197 | | component.componentId = int.Parse(componentIdStr); |
| | 198 | | } |
| | 199 | |
|
| 0 | 200 | | components.Add(component); |
| | 201 | | } |
| | 202 | |
|
| 0 | 203 | | return components; |
| | 204 | | } |
| | 205 | |
|
| | 206 | | private static List<string> GetComponentsCommon() |
| | 207 | | { |
| | 208 | | // We get all the files that are proto |
| 0 | 209 | | DirectoryInfo dir = new DirectoryInfo(Application.dataPath + TEMPPATH_TO_COMPONENTS_DEFINITIONS + "/" + PATH |
| 0 | 210 | | FileInfo[] info = dir.GetFiles("*.proto"); |
| 0 | 211 | | List<string> components = new List<string>(); |
| | 212 | |
|
| 0 | 213 | | foreach (FileInfo file in info) |
| | 214 | | { |
| | 215 | | // We ensure that only proto files are converted, this shouldn't be necessary but just in case |
| 0 | 216 | | if (!file.Name.Contains(".proto")) |
| | 217 | | continue; |
| | 218 | |
|
| 0 | 219 | | components.Add(file.Name); |
| | 220 | | } |
| | 221 | |
|
| 0 | 222 | | return components; |
| | 223 | | } |
| | 224 | | [MenuItem("Decentraland/Protobuf/Regenerate models (For debugging)")] |
| | 225 | | public static void GenerateComponentCode() |
| | 226 | | { |
| 0 | 227 | | GenerateComponentCode("LocalMachine"); |
| 0 | 228 | | } |
| | 229 | |
|
| | 230 | | public static void GenerateComponentCode(string versionNameToCompile) |
| | 231 | | { |
| 0 | 232 | | Debug.Log("Starting regenerate "); |
| 0 | 233 | | bool ok = false; |
| | 234 | |
|
| 0 | 235 | | string tempOutputPath = Application.dataPath + PATH_TO_COMPONENTS + "temp"; |
| | 236 | | try |
| | 237 | | { |
| 0 | 238 | | List<ProtoComponent> components = GetComponents(); |
| 0 | 239 | | components = components.OrderBy( component => component.componentId).ToList(); |
| | 240 | |
|
| 0 | 241 | | if (Directory.Exists(tempOutputPath)) |
| | 242 | | { |
| 0 | 243 | | Directory.Delete(tempOutputPath, true); |
| | 244 | | } |
| 0 | 245 | | Directory.CreateDirectory(tempOutputPath); |
| 0 | 246 | | Directory.CreateDirectory(tempOutputPath + SUBPATH_TO_COMPONENTS_COMMON); |
| | 247 | |
|
| 0 | 248 | | CreateTempDefinitions(); |
| 0 | 249 | | AddNamespaceAndPackage(); |
| | 250 | |
|
| 0 | 251 | | ok = CompileAllComponents(components, tempOutputPath); |
| 0 | 252 | | ok &= CompileComponentsCommon(tempOutputPath + SUBPATH_TO_COMPONENTS_COMMON); |
| | 253 | |
|
| 0 | 254 | | if (ok) |
| 0 | 255 | | GenerateComponentIdEnum(components); |
| 0 | 256 | | } |
| 0 | 257 | | catch (Exception e) |
| | 258 | | { |
| 0 | 259 | | Debug.LogError("The component code generation has failed: " + e.Message); |
| 0 | 260 | | } |
| | 261 | |
|
| 0 | 262 | | if (ok) |
| | 263 | | { |
| 0 | 264 | | string outputPath = Application.dataPath + PATH_TO_COMPONENTS; |
| 0 | 265 | | if (Directory.Exists(outputPath)) |
| 0 | 266 | | Directory.Delete(outputPath, true); |
| | 267 | |
|
| 0 | 268 | | Directory.Move(tempOutputPath, outputPath); |
| | 269 | |
|
| 0 | 270 | | string path = Application.dataPath + PATH_TO_FOLDER; |
| 0 | 271 | | WriteVersion(versionNameToCompile, COMPILED_VERSION_FILENAME, path); |
| 0 | 272 | | } |
| 0 | 273 | | else if (Directory.Exists(tempOutputPath)) |
| | 274 | | { |
| 0 | 275 | | Directory.Delete(tempOutputPath, true); |
| | 276 | | } |
| | 277 | |
|
| 0 | 278 | | if (Directory.Exists(Application.dataPath + TEMPPATH_TO_COMPONENTS_DEFINITIONS)) { |
| 0 | 279 | | Directory.Delete(Application.dataPath + TEMPPATH_TO_COMPONENTS_DEFINITIONS, true); |
| | 280 | | } |
| 0 | 281 | | } |
| | 282 | |
|
| | 283 | | private static void CreateTempDefinitions() |
| | 284 | | { |
| 0 | 285 | | if (Directory.Exists(Application.dataPath + TEMPPATH_TO_COMPONENTS_DEFINITIONS)) |
| 0 | 286 | | Directory.Delete(Application.dataPath + TEMPPATH_TO_COMPONENTS_DEFINITIONS, true); |
| | 287 | |
|
| 0 | 288 | | ProtobufEditorHelper.CloneDirectory(Application.dataPath + REALPATH_TO_COMPONENTS_DEFINITIONS, Application.d |
| 0 | 289 | | } |
| | 290 | |
|
| | 291 | | private static void GenerateComponentIdEnum(List<ProtoComponent> components) |
| | 292 | | { |
| 0 | 293 | | string componentCsFileContent = "/* Autogenerated file, DO NOT EDIT! */\n\nnamespace DCL.ECS7\n{\n public |
| | 294 | |
|
| 0 | 295 | | componentCsFileContent += $" public const int TRANSFORM = 1;\n"; |
| 0 | 296 | | foreach (ProtoComponent component in components ) |
| | 297 | | { |
| 0 | 298 | | string componentUpperCaseName = ProtobufEditorHelper.ToSnakeCase(component.componentName).ToUpper(); |
| | 299 | |
|
| | 300 | | // Special case where NFT is created from NFTProto, instead of N_F_T_SHAPE we set NFT_SHAPE |
| 0 | 301 | | if (componentUpperCaseName.Contains("N_F_T")) |
| 0 | 302 | | componentUpperCaseName = "NFT_SHAPE"; |
| | 303 | |
|
| | 304 | | // Special case where GLTF is created from GLTFProto, instead of G_L_T_F_SHAPE we set GLTF_SHAPE |
| 0 | 305 | | if (componentUpperCaseName.Contains("G_L_T_F")) |
| 0 | 306 | | componentUpperCaseName = "GLTF_SHAPE"; |
| | 307 | |
|
| 0 | 308 | | componentCsFileContent += $" public const int {componentUpperCaseName} = {component.componentId.T |
| | 309 | | } |
| 0 | 310 | | componentCsFileContent += " }\n}\n"; |
| | 311 | |
|
| 0 | 312 | | File.WriteAllText(Application.dataPath + PATH_TO_COMPONENT_IDS, componentCsFileContent); |
| 0 | 313 | | } |
| | 314 | |
|
| | 315 | | private static bool CompileAllComponents(List<ProtoComponent> components, string outputPath) |
| | 316 | | { |
| 0 | 317 | | if (components.Count == 0) |
| | 318 | | { |
| 0 | 319 | | UnityEngine.Debug.LogError("There are no components to generate!!"); |
| 0 | 320 | | return false; |
| | 321 | | } |
| | 322 | |
|
| | 323 | | // We prepare the paths for the conversion |
| 0 | 324 | | string filePath = Application.dataPath + TEMPPATH_TO_COMPONENTS_DEFINITIONS; |
| | 325 | |
|
| 0 | 326 | | List<string> paramsArray = new List<string> |
| | 327 | | { |
| | 328 | | $"--csharp_out \"{outputPath}\"", |
| | 329 | | $"--proto_path \"{filePath}\"" |
| | 330 | | }; |
| | 331 | |
|
| 0 | 332 | | foreach(ProtoComponent component in components) |
| | 333 | | { |
| 0 | 334 | | paramsArray.Add($"\"{filePath}/{component.componentName}.proto\""); |
| | 335 | | } |
| | 336 | |
|
| 0 | 337 | | return ExecProtoCompilerCommand(string.Join(" ", paramsArray)); |
| | 338 | | } |
| | 339 | |
|
| | 340 | | private static bool CompileComponentsCommon(string outputPath) |
| | 341 | | { |
| 0 | 342 | | List<string> commonFiles = GetComponentsCommon(); |
| | 343 | |
|
| 0 | 344 | | if (commonFiles.Count == 0) |
| 0 | 345 | | return true; |
| | 346 | |
|
| | 347 | | // We prepare the paths for the conversion |
| 0 | 348 | | string filePath = Application.dataPath + TEMPPATH_TO_COMPONENTS_DEFINITIONS + "/" + PATHNAME_TO_COMPONENTS_D |
| | 349 | |
|
| 0 | 350 | | List<string> paramsArray = new List<string> |
| | 351 | | { |
| | 352 | | $"--csharp_out \"{outputPath}\"", |
| | 353 | | $"--proto_path \"{filePath}\"" |
| | 354 | | }; |
| | 355 | |
|
| 0 | 356 | | foreach(string protoFile in commonFiles) |
| | 357 | | { |
| 0 | 358 | | paramsArray.Add($"\"{filePath}/{protoFile}\""); |
| | 359 | | } |
| | 360 | |
|
| 0 | 361 | | return ExecProtoCompilerCommand(string.Join(" ", paramsArray)); |
| | 362 | | } |
| | 363 | |
|
| | 364 | | private static bool ExecProtoCompilerCommand(string finalArguments) |
| | 365 | | { |
| 0 | 366 | | string proto_path = GetPathToProto(); |
| | 367 | |
|
| | 368 | | // This is the console to convert the proto |
| 0 | 369 | | ProcessStartInfo startInfo = new ProcessStartInfo() { FileName = proto_path, Arguments = finalArguments }; |
| | 370 | |
|
| 0 | 371 | | Process proc = new Process() { StartInfo = startInfo }; |
| 0 | 372 | | proc.StartInfo.UseShellExecute = false; |
| 0 | 373 | | proc.StartInfo.RedirectStandardOutput = true; |
| 0 | 374 | | proc.StartInfo.RedirectStandardError = true; |
| 0 | 375 | | proc.Start(); |
| | 376 | |
|
| 0 | 377 | | string error = proc.StandardError.ReadToEnd(); |
| 0 | 378 | | proc.WaitForExit(); |
| | 379 | |
|
| 0 | 380 | | if (error != "") |
| | 381 | | { |
| 0 | 382 | | UnityEngine.Debug.LogError("Protobuf Unity failed : " + error); |
| 0 | 383 | | return false; |
| | 384 | | } |
| 0 | 385 | | return true; |
| | 386 | | } |
| | 387 | |
|
| | 388 | | private static void AddNamespaceAndPackage() |
| | 389 | | { |
| | 390 | | // We get all the files that are proto |
| 0 | 391 | | DirectoryInfo dir = new DirectoryInfo(Application.dataPath + TEMPPATH_TO_COMPONENTS_DEFINITIONS); |
| 0 | 392 | | FileInfo[] info = dir.GetFiles("*.proto"); |
| | 393 | |
|
| 0 | 394 | | foreach (FileInfo file in info) |
| | 395 | | { |
| | 396 | | // We ensure that only proto files are converted, this shouldn't be necessary but just in case |
| 0 | 397 | | if (!file.Name.Contains(".proto")) |
| | 398 | | continue; |
| | 399 | |
|
| 0 | 400 | | string protoContent = File.ReadAllText(file.FullName); |
| 0 | 401 | | List<string> lines = protoContent.Split('\n').ToList(); |
| 0 | 402 | | List<string> outLines = new List<string>(); |
| | 403 | |
|
| 0 | 404 | | foreach ( string line in lines ) |
| | 405 | | { |
| 0 | 406 | | if (line.IndexOf(PATHNAME_TO_COMPONENTS_DEFINITIONS_COMMON + "/id.proto") == -1 && line.IndexOf("(ec |
| | 407 | | { |
| 0 | 408 | | outLines.Add(line); |
| | 409 | | } |
| | 410 | | } |
| | 411 | |
|
| 0 | 412 | | outLines.Add("package decentraland.ecs;"); |
| 0 | 413 | | outLines.Add("option csharp_namespace = \"DCL.ECSComponents\";"); |
| | 414 | |
|
| 0 | 415 | | File.WriteAllLines(file.FullName, outLines.ToArray()); |
| | 416 | | } |
| 0 | 417 | | } |
| | 418 | |
|
| | 419 | | private static bool IsProtoVersionValid() |
| | 420 | | { |
| 0 | 421 | | string path = Application.dataPath + PATH_TO_GENERATED + EXECUTABLE_VERSION_FILENAME; |
| 0 | 422 | | string version = GetVersion(path); |
| 0 | 423 | | string protoPath = GetPathToProto(); |
| | 424 | |
|
| | 425 | | // If we are in windows, we add the extension of the file |
| | 426 | | #if UNITY_EDITOR_WIN |
| | 427 | | protoPath += ".exe"; |
| | 428 | | #endif |
| 0 | 429 | | return version == PROTO_VERSION && File.Exists(protoPath); |
| | 430 | | } |
| | 431 | |
|
| | 432 | | private static string GetVersion(string path) |
| | 433 | | { |
| 0 | 434 | | if (!File.Exists(path)) |
| 0 | 435 | | return ""; |
| | 436 | |
|
| 0 | 437 | | StreamReader reader = new StreamReader(path); |
| 0 | 438 | | string version = reader.ReadToEnd(); |
| 0 | 439 | | reader.Close(); |
| | 440 | |
|
| 0 | 441 | | return version; |
| | 442 | | } |
| | 443 | |
|
| | 444 | | private static void WriteVersion(string version, string filename) |
| | 445 | | { |
| 0 | 446 | | string path = Application.dataPath + PATH_TO_GENERATED + "/"; |
| 0 | 447 | | WriteVersion(version, filename, path); |
| 0 | 448 | | } |
| | 449 | |
|
| | 450 | | private static void WriteVersion(string version, string filename, string path) |
| | 451 | | { |
| 0 | 452 | | string filePath = path + filename; |
| 0 | 453 | | var sr = File.CreateText(filePath); |
| 0 | 454 | | sr.Write(version); |
| 0 | 455 | | sr.Close(); |
| 0 | 456 | | } |
| | 457 | |
|
| | 458 | | private static string GetDownloadedVersion() |
| | 459 | | { |
| 0 | 460 | | string path = Application.dataPath + PATH_TO_GENERATED + "/" + DOWNLOADED_VERSION_FILENAME; |
| 0 | 461 | | return GetVersion(path); |
| | 462 | | } |
| | 463 | |
|
| | 464 | | private static string GetCompiledVersion() |
| | 465 | | { |
| 0 | 466 | | string path = Application.dataPath + PATH_TO_FOLDER + COMPILED_VERSION_FILENAME; |
| 0 | 467 | | return GetVersion(path); |
| | 468 | | } |
| | 469 | |
|
| | 470 | | public static string GetLatestProtoVersion() |
| | 471 | | { |
| | 472 | | WebClient client; |
| | 473 | | Stream data; |
| | 474 | | StreamReader reader; |
| | 475 | | string libraryJsonString; |
| | 476 | | Dictionary<string, object> libraryContent, libraryInfo; |
| | 477 | |
|
| | 478 | | // Download the data of decentraland-/ecs |
| 0 | 479 | | client = new WebClient(); |
| 0 | 480 | | data = client.OpenRead(@"https://registry.npmjs.org/" + NPM_PACKAGE); |
| 0 | 481 | | if (data == null) |
| | 482 | | { |
| 0 | 483 | | return ""; |
| | 484 | | } |
| | 485 | |
|
| 0 | 486 | | reader = new StreamReader(data); |
| 0 | 487 | | libraryJsonString = reader.ReadToEnd(); |
| 0 | 488 | | data.Close(); |
| 0 | 489 | | reader.Close(); |
| | 490 | |
|
| | 491 | | // Process the response |
| 0 | 492 | | libraryContent = JsonConvert.DeserializeObject<Dictionary<string, object>>(libraryJsonString); |
| 0 | 493 | | libraryInfo = JsonConvert.DeserializeObject<Dictionary<string, object>>(libraryContent["dist-tags"].ToString |
| | 494 | |
|
| 0 | 495 | | string nextVersion = libraryInfo["next"].ToString(); |
| 0 | 496 | | return nextVersion; |
| | 497 | | } |
| | 498 | |
|
| | 499 | | [MenuItem("Decentraland/Protobuf/Download proto executable")] |
| | 500 | | public static void DownloadProtobuffExecutable() |
| | 501 | | { |
| | 502 | | // Download package |
| 0 | 503 | | string machine = null; |
| 0 | 504 | | string executableName = "protoc"; |
| | 505 | | #if UNITY_EDITOR_WIN |
| | 506 | | machine = "win64"; |
| | 507 | | executableName = "protoc.exe"; |
| | 508 | | #elif UNITY_EDITOR_OSX |
| | 509 | | machine = "osx-x86_64"; |
| | 510 | | #elif UNITY_EDITOR_LINUX |
| 0 | 511 | | machine = "linux-x86_64"; |
| | 512 | | #endif |
| | 513 | | // We download the proto executable |
| 0 | 514 | | string name = $"protoc-{PROTO_VERSION}-{machine}.zip"; |
| 0 | 515 | | string url = $"https://github.com/protocolbuffers/protobuf/releases/download/v{PROTO_VERSION}/{name}"; |
| 0 | 516 | | string zipProtoFileName = "protoc"; |
| 0 | 517 | | WebClient client = new WebClient(); |
| 0 | 518 | | client.DownloadFile(url, zipProtoFileName); |
| 0 | 519 | | string destPackage = "protobuf"; |
| | 520 | |
|
| | 521 | | try |
| | 522 | | { |
| 0 | 523 | | Directory.CreateDirectory(destPackage); |
| | 524 | |
|
| | 525 | | // We unzip the proto executable |
| 0 | 526 | | Unzip(zipProtoFileName,destPackage); |
| | 527 | |
|
| | 528 | | if (VERBOSE) |
| 0 | 529 | | UnityEngine.Debug.Log("Unzipped protoc"); |
| | 530 | |
|
| 0 | 531 | | string outputPathDir = Application.dataPath + PATH_TO_PROTO ; |
| 0 | 532 | | string outputPath = outputPathDir + executableName; |
| | 533 | |
|
| 0 | 534 | | if (File.Exists(outputPath)) |
| 0 | 535 | | File.Delete(outputPath); |
| | 536 | |
|
| 0 | 537 | | if (!Directory.Exists(outputPathDir)) |
| 0 | 538 | | Directory.CreateDirectory(outputPathDir); |
| | 539 | |
|
| | 540 | | // We move the executable to his correct path |
| 0 | 541 | | Directory.Move(destPackage + "/bin/" + executableName, outputPath); |
| | 542 | |
|
| | 543 | | #if UNITY_EDITOR_OSX || UNITY_EDITOR_LINUX |
| 0 | 544 | | AddExecutablePermisson(GetPathToProto()); |
| | 545 | | #endif |
| | 546 | |
|
| 0 | 547 | | WriteVersion(PROTO_VERSION, EXECUTABLE_VERSION_FILENAME); |
| 0 | 548 | | } |
| 0 | 549 | | catch (Exception e) |
| | 550 | | { |
| 0 | 551 | | Debug.LogError("The download of the executable has failed " + e.Message); |
| 0 | 552 | | } |
| | 553 | | finally |
| | 554 | | { |
| | 555 | | // We removed everything has has been created and it is not usefull anymore |
| 0 | 556 | | File.Delete(zipProtoFileName); |
| 0 | 557 | | if (Directory.Exists(destPackage)) |
| 0 | 558 | | Directory.Delete(destPackage, true); |
| 0 | 559 | | } |
| 0 | 560 | | } |
| | 561 | |
|
| | 562 | | private static string GetPathToProto() |
| | 563 | | { |
| 0 | 564 | | return Application.dataPath + PATH_TO_PROTO + PROTO_FILENAME; |
| | 565 | | } |
| | 566 | |
|
| | 567 | | #if UNITY_EDITOR_OSX || UNITY_EDITOR_LINUX |
| | 568 | | private static bool AddExecutablePermisson(string path) |
| | 569 | | { |
| | 570 | | // This is the console to convert the proto |
| 0 | 571 | | ProcessStartInfo startInfo = new ProcessStartInfo() { FileName = "chmod", Arguments = $"+x \"{path}\"" }; |
| | 572 | |
|
| 0 | 573 | | Process proc = new Process() { StartInfo = startInfo }; |
| 0 | 574 | | proc.StartInfo.UseShellExecute = false; |
| 0 | 575 | | proc.StartInfo.RedirectStandardOutput = true; |
| 0 | 576 | | proc.StartInfo.RedirectStandardError = true; |
| 0 | 577 | | proc.Start(); |
| | 578 | |
|
| 0 | 579 | | string error = proc.StandardError.ReadToEnd(); |
| 0 | 580 | | proc.WaitForExit(); |
| | 581 | |
|
| 0 | 582 | | if (error != "") |
| | 583 | | { |
| 0 | 584 | | UnityEngine.Debug.LogError("`chmod +x protoc` failed : " + error); |
| 0 | 585 | | return false; |
| | 586 | | } |
| 0 | 587 | | return true; |
| | 588 | | } |
| | 589 | | #endif |
| | 590 | |
|
| | 591 | |
|
| | 592 | | private static void Untar(string name, string path) |
| | 593 | | { |
| 0 | 594 | | using (Stream inStream = File.OpenRead (name)) |
| 0 | 595 | | using (Stream gzipStream = new GZipInputStream (inStream)) { |
| 0 | 596 | | TarArchive tarArchive = TarArchive.CreateInputTarArchive(gzipStream, Encoding.ASCII); |
| 0 | 597 | | tarArchive.ExtractContents (path); |
| 0 | 598 | | } |
| 0 | 599 | | } |
| | 600 | |
|
| | 601 | | private static void Unzip(string name, string path) |
| | 602 | | { |
| 0 | 603 | | FastZip fastZip = new FastZip(); |
| 0 | 604 | | string fileFilter = null; |
| | 605 | |
|
| 0 | 606 | | fastZip.ExtractZip(name, path, fileFilter); |
| 0 | 607 | | } |
| | 608 | |
|
| | 609 | | private static void OnProjectCompile(object test) |
| | 610 | | { |
| 0 | 611 | | OnProjectCompile(); |
| 0 | 612 | | } |
| | 613 | |
|
| | 614 | | [MenuItem("Decentraland/Protobuf/Test project compile (For debugging)")] |
| | 615 | | private static void OnProjectCompile() |
| | 616 | | { |
| | 617 | | // TODO: Delete this return line to make the generation of the proto based on your machine |
| 0 | 618 | | return; |
| | 619 | |
|
| | 620 | | // The compiled version is a file that lives in the repo, if your local version is distinct it will generate |
| | 621 | | var currentDownloadedVersion = GetDownloadedVersion(); |
| | 622 | | var currentVersion = GetCompiledVersion(); |
| | 623 | | if (currentVersion != currentDownloadedVersion) |
| | 624 | | UpdateModels(currentVersion); |
| | 625 | | } |
| | 626 | | } |
| | 627 | | } |