diff --git a/README.md b/README.md index 0e742fff..4dca211f 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ Supported Languages: 中文 / [繁體中文](README-tw.md) / [English](README-en 7. `选择模式`由状态栏移动到`选项`菜单中。 8. 修复`点击场景添加模型`,添加过程中不出现模型预览bug。 9. `app.editor.scripts`由`Object`改为`Array`,不会对以前的场景造成影响。 -10. 场景脚本第三方编辑器支持,场景中创建的脚本自动同步到`SceneScript`文件夹中。 +10. 场景脚本第三方编辑器支持,场景中创建的脚本自动同步到`SceneScript`文件夹中。(未完成) ## v0.4.4更新【[更新日志](docs-dev/update/UpdateLog.md)】 diff --git a/ShadowEditor.Server/Remote/RemoteHelper.cs b/ShadowEditor.Server/Remote/RemoteHelper.cs new file mode 100644 index 00000000..1dfb119f --- /dev/null +++ b/ShadowEditor.Server/Remote/RemoteHelper.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.IO; + +namespace ShadowEditor.Server.Remote +{ + /// + /// 远程编辑帮助函数 + /// + public class RemoteHelper + { + /// + /// 获取远程编辑临时路径 + /// + /// + public static string GetRemotePath() + { + var path = AppDomain.CurrentDomain.BaseDirectory; + if (path.EndsWith("\\")) + { + path = path.Substring(0, path.Length - 1); + } + + var paths = path.Split('\\').ToList(); + paths.RemoveAt(paths.Count - 1); + paths.Add("SceneScript"); + + path = string.Join("\\", paths); + + if (!Directory.Exists(path)) + { + Directory.CreateDirectory(path); + } + + return path; + } + } +} diff --git a/ShadowEditor.Server/Remote/RemoteServer.cs b/ShadowEditor.Server/Remote/RemoteServer.cs index f4881b70..cc96bebf 100644 --- a/ShadowEditor.Server/Remote/RemoteServer.cs +++ b/ShadowEditor.Server/Remote/RemoteServer.cs @@ -28,7 +28,9 @@ namespace ShadowEditor.Server.Remote { // see: https://github.com/jjrdk/websocket-sharp webSocketServer = new WebSocketServer(null, ConfigHelper.WebSocketServerPort); - webSocketServer.AddWebSocketService("/RemoteEdit"); + + webSocketServer.AddWebSocketService("/RemoteEdit"); + webSocketServer.Start(); } catch (Exception ex) diff --git a/ShadowEditor.Server/Remote/SocketServer.cs b/ShadowEditor.Server/Remote/RemoteSocket.cs similarity index 60% rename from ShadowEditor.Server/Remote/SocketServer.cs rename to ShadowEditor.Server/Remote/RemoteSocket.cs index 280c5801..5cff3f63 100644 --- a/ShadowEditor.Server/Remote/SocketServer.cs +++ b/ShadowEditor.Server/Remote/RemoteSocket.cs @@ -8,46 +8,95 @@ using System.Web; using WebSocketSharp; using WebSocketSharp.Server; using ShadowEditor.Model.Script; +using ShadowEditor.Server.Helpers; using Newtonsoft.Json; using Newtonsoft.Json.Linq; namespace ShadowEditor.Server.Remote { /// - /// Socket服务端 + /// Remote Socket /// /// - public class SocketServer : WebSocketBehavior + public class RemoteSocket : WebSocketBehavior { + private string rootPath = RemoteHelper.GetRemotePath(); + + public RemoteSocket() : base() + { + var path = RemoteHelper.GetRemotePath(); + try + { + var watcher = new FileSystemWatcher(path); + watcher.IncludeSubdirectories = true; + watcher.Changed += OnFileChanged; + } + catch (Exception ex) + { + var log = LogHelper.GetLogger(this.GetType()); + log.Error("Create FileSystemWatcher failed.", ex); + } + } + + protected override Task OnOpen() + { + var log = LogHelper.GetLogger(this.GetType()); + log.Info("Socket open."); + return base.OnOpen(); + } + protected override Task OnMessage(MessageEventArgs e) { - var path = AppDomain.CurrentDomain.BaseDirectory; - if (path.EndsWith("\\")) - { - path = path.Substring(0, path.Length - 1); - } - - var paths = path.Split('\\').ToList(); - paths.RemoveAt(paths.Count - 1); - paths.Add("SceneScript"); - - path = string.Join("\\", paths); - - if (!Directory.Exists(path)) - { - Directory.CreateDirectory(path); - } - var list = GetScripts(e.Data); - DeleteScripts(path, true); + DeleteScripts(rootPath, true); - CreateScripts("", path, list); + CreateScripts("", rootPath, list); + + Send(JsonConvert.SerializeObject(new + { + Code = 200, + Msg = "Send Successfully!", + Type = "Response" + })); - Send("Hello, world!"); return base.OnMessage(e); } + protected override Task OnClose(CloseEventArgs e) + { + var log = LogHelper.GetLogger(this.GetType()); + log.Warn("Socket close."); + return base.OnClose(e); + } + + protected override Task OnError(WebSocketSharp.ErrorEventArgs e) + { + var log = LogHelper.GetLogger(this.GetType()); + log.Error(e.Message, e.Exception); + return base.OnError(e); + } + + /// + /// 远程编辑文件发生改变 + /// + /// + /// + public void OnFileChanged(object sender, FileSystemEventArgs e) + { + var list = new List(); + + TraverseFolder(rootPath, list); + + Send(JsonConvert.SerializeObject(new + { + Code = 200, + Msg = "Send Successfully!", + Type = "Response" + })); + } + + #region 场景脚本改变更新SceneScript文件夹 private List GetScripts(Stream stream) { var reader = new StreamReader(stream, Encoding.UTF8); @@ -156,5 +205,23 @@ namespace ShadowEditor.Server.Remote return extension; } + #endregion + + #region SceneScript文件夹改变更新场景脚本 + public void TraverseFolder(string path, List scripts) + { + var directories = Directory.GetDirectories(path); + var files = Directory.GetFiles(path); + + foreach (var i in directories) + { + + } + foreach (var i in files) + { + + } + } + #endregion } } diff --git a/ShadowEditor.Server/ShadowEditor.Server.csproj b/ShadowEditor.Server/ShadowEditor.Server.csproj index fee81430..638c737c 100644 --- a/ShadowEditor.Server/ShadowEditor.Server.csproj +++ b/ShadowEditor.Server/ShadowEditor.Server.csproj @@ -216,8 +216,9 @@ + - +