/// <summary> /// Called every update call of Unity /// /// THREADSAFETY -> In this thread we can do stuff with Unity! /// </summary> public void OnUpdate() { // lock (lockThis) { string origin = UnityEngine.Application.dataPath.Replace("\\", "/"); // SAVE Asset & Script (in the Unity Thread) try{ foreach (SaveEntry assetToSave in specificAssetsToSave) { string path = assetToSave.filename; string content = assetToSave.content; string codecontent = assetToSave.code; // Update Vubbi file VubbiFileHandler.UpdateVubbiScriptContent(path, content); // Save the generated code string fullpathcs = path + ".cs"; using (StreamWriter sw = new StreamWriter(fullpathcs)) { sw.Write(codecontent); } AssetDatabase.ImportAsset(path + ".cs"); } } finally { specificAssetsToSave.Clear(); } // LOAD Asset & Script (in the Unity Thread) try{ foreach (LoadEntry assetToLoad in specificAssetsToLoad) { assetToLoad.content = VubbiFileHandler.GetVubbiScriptContent(assetToLoad.filename); ((AutoResetEvent)assetToLoad.waitHandle).Set(); } } finally { specificAssetsToLoad.Clear(); } // CHECK Preferences... if (langPrefToSave) { langPrefToSave = false; EditorPrefs.SetString("VubbiScriptLanguagePreference", langPref); } if (!langPrefLoaded) { langPrefLoaded = true; string langPrefTemp = EditorPrefs.GetString("VubbiScriptLanguagePreference"); if (langPrefTemp == null) { langPrefTemp = ""; } langPref = langPrefTemp; } } }
// // Data migration v2 -> v3 // // REMOVED FROM MENU!! => [MenuItem("Help/Migrate Scratchity v0.1 & v0.2 Data", false, 500)] public static void MigrationData() { string oldDataDir = Path.Combine(UnityEngine.Application.dataPath, "../BlockScriptData"); string scriptDir = Path.Combine(UnityEngine.Application.dataPath, "Scripts"); if (Directory.Exists(oldDataDir)) { string[] files = Directory.GetFiles(oldDataDir); foreach (string file in files) { if (".xml".Equals(Path.GetExtension(file)) && ".vcs".Equals(Path.GetExtension(Path.GetFileNameWithoutExtension(file)))) { UnityEngine.Debug.Log("Migrating file: " + file); string actualname = Path.GetFileNameWithoutExtension(Path.GetFileNameWithoutExtension(file)); string filecontent = File.ReadAllText(file); VisualBlockScript dataObj = VubbiFileHandler.CreateAsset("Assets/Scripts", actualname, false); dataObj.data = filecontent; EditorUtility.SetDirty(dataObj); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); } } // For safety reasons, do NOT DELETE OLD DATA from before migration! // Directory.Delete (oldDataDir, true); } }
// // Methods... // /// <summary> /// Save a file (& generate C# code) /// /// Called by an incoming http connection => /// THREADSAFETY: Called from web server thread => do not call Unity code from this thread! /// </summary> /// <param name="data">Data.</param> public void Save(Dictionary <string, string> data) { string filename = data ["file"]; string content = data ["content"]; string codecontent = data ["code"]; ServerLog.Log("Saving \"" + filename + "\""); string fullpath = Path.Combine(scriptOutputDir, filename); string directory = Path.GetDirectoryName(fullpath); // Save the file Directory.CreateDirectory(directory); SaveEntry entry = new SaveEntry(); entry.filename = "Assets/" + filename; entry.content = content; entry.code = VubbiFileHandler.GenerateHeader() + codecontent; lock (lockThis) { specificAssetsToSave.Add(entry); } // Note: if the save throws an error we will never know (we are in a different thread and we are not waiting for the save) }
public void ModalClosed(ModalWindow window) { VisualBlockFileNameWindow rename = window as VisualBlockFileNameWindow; if (rename == null || window.Result != WindowResult.Ok) { return; } string filename = rename.Texts[0]; VubbiFileHandler.CreateNewAssetWithName(usePath, filename); }
/// <summary> /// /// This code is called when the Unity Editor starts. It starts Vubbi (installation + web server). /// /// </summary> static Init() { // // Check command line arguments... // string commandlinebuildkeyword = "+exportvubbiscriptunitypackage"; if (System.Environment.GetCommandLineArgs().Contains(commandlinebuildkeyword)) { // So, we need to do a build instead of running the code... string[] cargs = System.Environment.GetCommandLineArgs(); string outputdir = null; for (int i = 0; i < cargs.Length; i++) { if (cargs [i] == commandlinebuildkeyword) { outputdir = cargs [i + 1]; } } AssetDatabase.ExportPackage("Assets/Editor Default Resources/Editor/VubbiScript", outputdir, ExportPackageOptions.Recurse); return; // DO NOT CONTINUE... Exit Unity now... } // // Set the icon for Vubbi files // VubbiFileHandler.InitVubbiIcon(); // // Server Configuration // ServerConfig config = new ServerConfig(); #if UNITY_EDITOR // When running in development mode => the "web" code can be found in the folder "web" next to the actual unity project folder config.HostDirectory = System.IO.Path.Combine(UnityEngine.Application.dataPath, "../../web"); #else // When compiling for the release build, we do not define UNITY_EDITOR // For released code, the HTML & JavaScript can be found in: config.HostDirectory = System.IO.Path.Combine(UnityEngine.Application.dataPath, "../WebRoot"); #endif config.Port = 8040; // // Update Web directory // This is part of the "installer"... // Right after importing the unity package, we will unzip the data file and put it in the main project folder. // => Resulting in a "WebRoot" folder // string dataFile = System.IO.Path.Combine(UnityEngine.Application.dataPath, "Editor Default Resources/Editor/VubbiScript/Vubbi.data"); if (System.IO.File.Exists(dataFile)) { System.IO.Directory.CreateDirectory(config.HostDirectory + "/"); UnzipDataDir(dataFile, config.HostDirectory + "/"); System.IO.File.Delete(dataFile); AssetDatabase.Refresh(); ServerLog.Log("Installation successful!"); } // // Start Server // VubbiApi api = new VubbiApi(); new VubbiServer(config, api); // // Register update callback // EditorApplication.update += api.OnUpdate; }