Пример #1
0
 public LuaArgs open(LuaArgs args)
 {
     if (args.IsFunction(0))
     {
         // Open a function
         var function = args.GetFunction(0);
         var mode     = args.IsNil(1) ? "r" : args.GetString(1);
         if (mode == "r" || mode == "rb")
         {
             var stream = new LuaFunctionStream(function, LuaFileOpenMode.Read);
             return(new LuaArgs(new LuaFile(stream, LuaFileOpenMode.Read)));
         }
         else if (mode == "w" || mode == "a" || mode == "wb" || mode == "ab")
         {
             var stream = new LuaFunctionStream(function, LuaFileOpenMode.Write);
             return(new LuaArgs(new LuaFile(stream, LuaFileOpenMode.Write)));
         }
         else
         {
             throw new LuaError("Unsupported mode: " + mode);
         }
     }
     else
     {
         // Open a file
         var path = new FilePath(args.GetString(0));
         var mode = args.IsNil(1) ? "r" : args.GetString(1);
         try
         {
             if (mode == "r")
             {
                 return(new LuaArgs(new LuaFile(m_fileSystem.OpenForRead(path))));
             }
             else if (mode == "rb")
             {
                 return(new LuaArgs(new LuaFile(m_fileSystem.OpenForBinaryRead(path), LuaFileOpenMode.Read)));
             }
             else if (mode == "w")
             {
                 return(new LuaArgs(new LuaFile(m_fileSystem.OpenForWrite(path, false))));
             }
             else if (mode == "wb")
             {
                 return(new LuaArgs(new LuaFile(m_fileSystem.OpenForBinaryWrite(path, false), LuaFileOpenMode.Write)));
             }
             else if (mode == "a")
             {
                 return(new LuaArgs(new LuaFile(m_fileSystem.OpenForWrite(path, true))));
             }
             else if (mode == "ab")
             {
                 return(new LuaArgs(new LuaFile(m_fileSystem.OpenForBinaryWrite(path, true), LuaFileOpenMode.Write)));
             }
             else
             {
                 throw new LuaError("Unsupported mode: " + mode);
             }
         }
         catch (FileNotFoundException)
         {
             return(LuaArgs.Nil);
         }
         catch (IOException e)
         {
             throw new LuaError(e.Message);
         }
     }
 }