OsSystemModule.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using MoonSharp.Interpreter.Execution;
  8. namespace MoonSharp.Interpreter.CoreLib
  9. {
  10. /// <summary>
  11. /// Class implementing system related Lua functions from the 'os' module.
  12. /// Proper support requires a compatible IPlatformAccessor
  13. /// </summary>
  14. [MoonSharpModule(Namespace = "os")]
  15. public class OsSystemModule
  16. {
  17. [MoonSharpModuleMethod]
  18. public static DynValue execute(ScriptExecutionContext executionContext, CallbackArguments args)
  19. {
  20. DynValue v = args.AsType(0, "execute", DataType.String, true);
  21. if (v.IsNil())
  22. {
  23. return DynValue.NewBoolean(true);
  24. }
  25. else
  26. {
  27. try
  28. {
  29. int exitCode = Script.GlobalOptions.Platform.OS_Execute(v.String);
  30. return DynValue.NewTuple(
  31. DynValue.Nil,
  32. DynValue.NewString("exit"),
  33. DynValue.NewNumber(exitCode));
  34. }
  35. catch (Exception)
  36. {
  37. // +++ bad to swallow..
  38. return DynValue.Nil;
  39. }
  40. }
  41. }
  42. [MoonSharpModuleMethod]
  43. public static DynValue exit(ScriptExecutionContext executionContext, CallbackArguments args)
  44. {
  45. DynValue v_exitCode = args.AsType(0, "exit", DataType.Number, true);
  46. int exitCode = 0;
  47. if (v_exitCode.IsNotNil())
  48. exitCode = (int)v_exitCode.Number;
  49. Script.GlobalOptions.Platform.OS_ExitFast(exitCode);
  50. throw new InvalidOperationException("Unreachable code.. reached.");
  51. }
  52. [MoonSharpModuleMethod]
  53. public static DynValue getenv(ScriptExecutionContext executionContext, CallbackArguments args)
  54. {
  55. DynValue varName = args.AsType(0, "getenv", DataType.String, false);
  56. string val = Script.GlobalOptions.Platform.GetEnvironmentVariable(varName.String);
  57. if (val == null)
  58. return DynValue.Nil;
  59. else
  60. return DynValue.NewString(val);
  61. }
  62. [MoonSharpModuleMethod]
  63. public static DynValue remove(ScriptExecutionContext executionContext, CallbackArguments args)
  64. {
  65. string fileName = args.AsType(0, "remove", DataType.String, false).String;
  66. try
  67. {
  68. if (Script.GlobalOptions.Platform.OS_FileExists(fileName))
  69. {
  70. Script.GlobalOptions.Platform.OS_FileDelete(fileName);
  71. return DynValue.True;
  72. }
  73. else
  74. {
  75. return DynValue.NewTuple(
  76. DynValue.Nil,
  77. DynValue.NewString("{0}: No such file or directory.", fileName),
  78. DynValue.NewNumber(-1));
  79. }
  80. }
  81. catch (Exception ex)
  82. {
  83. return DynValue.NewTuple(DynValue.Nil, DynValue.NewString(ex.Message), DynValue.NewNumber(-1));
  84. }
  85. }
  86. [MoonSharpModuleMethod]
  87. public static DynValue rename(ScriptExecutionContext executionContext, CallbackArguments args)
  88. {
  89. string fileNameOld = args.AsType(0, "rename", DataType.String, false).String;
  90. string fileNameNew = args.AsType(1, "rename", DataType.String, false).String;
  91. try
  92. {
  93. if (!Script.GlobalOptions.Platform.OS_FileExists(fileNameOld))
  94. {
  95. return DynValue.NewTuple(DynValue.Nil,
  96. DynValue.NewString("{0}: No such file or directory.", fileNameOld),
  97. DynValue.NewNumber(-1));
  98. }
  99. Script.GlobalOptions.Platform.OS_FileMove(fileNameOld, fileNameNew);
  100. return DynValue.True;
  101. }
  102. catch (Exception ex)
  103. {
  104. return DynValue.NewTuple(DynValue.Nil, DynValue.NewString(ex.Message), DynValue.NewNumber(-1));
  105. }
  106. }
  107. [MoonSharpModuleMethod]
  108. public static DynValue setlocale(ScriptExecutionContext executionContext, CallbackArguments args)
  109. {
  110. return DynValue.NewString("n/a");
  111. }
  112. [MoonSharpModuleMethod]
  113. public static DynValue tmpname(ScriptExecutionContext executionContext, CallbackArguments args)
  114. {
  115. return DynValue.NewString(Script.GlobalOptions.Platform.IO_OS_GetTempFilename());
  116. }
  117. }
  118. }