/// <summary> /// Given a template this creates a new session and assigns it. /// </summary> /// <param name="template">The template you want to load the session into.</param> protected void CreateSession(BaseTemplate template) { // Clear any old errors ClearError(ScriptForgeErrors.Codes.Missing_Session_Key); // Create the new session IDictionary <string, object> session = new TemplateSession(); // Populate it PopulateSession(session); // Assign it template.Session = session; // Try to run it. try { // Initialize it template.Initialize(); } catch (MissingSessionKeyException missingSessionKey) { DisplayError(ScriptForgeErrors.Codes.Missing_Session_Key, "The template is missing the session key '" + missingSessionKey.key + "' and can't be compiled."); } catch (System.Exception e) { DisplayError(ScriptForgeErrors.Codes.Other, "An exception was thrown when generating the code " + e.ToString()); } }
/// <summary> /// Given a system file path and a clss defintion this will write the contents /// to disk. If the folder does not exist one will be created for you. /// </summary> /// <param name="savePath">The system save path for the generated class.</param> /// <param name="classDefintion">The string content of the class.</param> protected void WriteToDisk(BaseTemplate template) { try { // Get our path string savePath = (string)template.Session["m_SaveLocation"]; // Get our directory string directory = Path.GetDirectoryName(savePath); // Check if it exists if (!Directory.Exists(directory)) { // Create one if it does not. Directory.CreateDirectory(directory); } // Build our text string classDefintion = template.TransformText(); // Save new class to assets folder. File.WriteAllText(savePath, classDefintion); // Refresh assets. AssetDatabase.Refresh(); } catch (System.Exception e) { Debug.LogError("An error occurred while saving file: " + e); } }