Skip to content

Commit

Permalink
More debug info and a small refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
andybak committed Nov 28, 2023
1 parent f5f258a commit b34476c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 18 deletions.
29 changes: 11 additions & 18 deletions Assets/Scripts/API/Lua/LuaManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,15 @@ public void Init()
Directory.CreateDirectory(LuaModulesPath);
}

// Allow includes from Scripts/LuaModules
Script.DefaultOptions.ScriptLoader = new FileSystemScriptLoader();
((ScriptLoaderBase)Script.DefaultOptions.ScriptLoader).ModulePaths = new[] { Path.Join(LuaModulesPath, "?.lua") };

CopyLuaModules();

// Allow includes from Scripts/LuaModules
Script.DefaultOptions.ScriptLoader = new OpenBrushScriptLoader();
((ScriptLoaderBase)Script.DefaultOptions.ScriptLoader).ModulePaths = new[]
{
Path.Join(LuaModulesPath, "?.lua")
};

LoadExampleScripts();
LoadUserScripts();

Expand Down Expand Up @@ -448,12 +451,12 @@ private string LoadScriptFromString(string scriptFilename, string contents, bool
}
catch (ScriptRuntimeException e)
{
LogLuaInterpreterError(script, $"(ScriptRuntimeException loading: {scriptFilename})", e);
LogLuaInterpreterError(script, $"(Runtime Error loading: {scriptFilename})", e);
return null;
}
catch (SyntaxErrorException e)
{
LogLuaInterpreterError(script, $"(SyntaxErrorException loading: {scriptFilename})", e);
LogLuaInterpreterError(script, $"(Syntax Error loading: {scriptFilename})", e);
return null;
}
var catMatch = TryGetCategoryFromScriptName(scriptFilename);
Expand All @@ -462,18 +465,8 @@ private string LoadScriptFromString(string scriptFilename, string contents, bool
var category = catMatch.Value;
scriptName = scriptFilename.Substring(category.ToString().Length + 1);

try {script.Globals[LuaNames.ScriptNameString] = scriptName;}
catch (ScriptRuntimeException e)
{
LogLuaInterpreterError(script, $"(ScriptRuntimeException setting ScriptNameString: {scriptFilename})", e);
return null;
}
try { script.Globals[LuaNames.IsExampleScriptBool] = isExampleScript; }
catch (ScriptRuntimeException e)
{
LogLuaInterpreterError(script, $"(ScriptRuntimeException setting IsExampleScriptBool: {scriptFilename})", e);
return null;
}
script.Globals[LuaNames.ScriptNameString] = scriptName;
script.Globals[LuaNames.IsExampleScriptBool] = isExampleScript;

Scripts[category][scriptName] = script;
if (m_ActiveBackgroundScripts.ContainsKey(scriptName))
Expand Down
37 changes: 37 additions & 0 deletions Assets/Scripts/API/Lua/OpenBrushScriptLoader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2023 The Open Brush Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using MoonSharp.Interpreter;
using MoonSharp.Interpreter.Loaders;
using System.IO;
using UnityEngine;

namespace TiltBrush
{
public class OpenBrushScriptLoader : ScriptLoaderBase
{
public override bool ScriptFileExists(string name)
{
Debug.Log($"ScriptFileExists: {name}? {File.Exists(name)}");
return File.Exists(name);
}

public override object LoadFile(string file, Table globalContext)
{
FileStream result = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
Debug.Log($"LoadFile: {file} CanRead? {result.CanRead}");
return result;
}
}
}
3 changes: 3 additions & 0 deletions Assets/Scripts/API/Lua/OpenBrushScriptLoader.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b34476c

Please sign in to comment.