コード例 #1
0
 public File(File parent, string path, string name, bool isMaster, bool isDir, InitialState initialState, MasterState masterState)
 {
     this.parent       = parent;
     this.path         = path.Replace('\\', '/');
     this.name         = Path.GetFileName(path);
     this.isDir        = isDir;
     this.initialState = initialState;
     this.masterState  = masterState;
     this.isMaster     = isMaster;
     this.initialHash  = isDir == false?NGSyncFoldersWindow.TryGetCachedHash(this.path) : string.Empty;
 }
コード例 #2
0
 public File(File parent, string path, string name, bool isMaster, bool isDir, InitialState state, SlaveState slaveState)
 {
     this.parent       = parent;
     this.path         = path.Replace('\\', '/');
     this.name         = name;
     this.isDir        = isDir;
     this.initialState = state;
     this.slaveState   = slaveState;
     this.isMaster     = isMaster;
     this.initialHash  = isDir == false?NGSyncFoldersWindow.TryGetCachedHash(this.path) : string.Empty;
 }
コード例 #3
0
        private void    Sync(File model, bool force = false)
        {
            Action action = this.ChooseAction(model);

            if (action != Action.Nothing || force == true)
            {
                try
                {
                    this.actionException = null;

                    if (action == Action.Delete)
                    {
                        System.IO.File.Delete(this.path);
                        EditorApplication.delayCall += () => this.parent.children.Remove(this);

                        if (Directory.GetFiles(Path.GetDirectoryName(this.path)).Length == 0)
                        {
                            Directory.Delete(Path.GetDirectoryName(this.path));
                        }

                        InternalNGDebug.InternalLogFormat("Sync \"{0}\": Deleted.", this.path);
                    }
                    else if (action == Action.Create)
                    {
                        Directory.CreateDirectory(Path.GetDirectoryName(this.path));
                        if (System.IO.File.Exists(this.path) == false)
                        {
                            System.IO.File.Copy(model.path, this.path);
                        }
                        else
                        {
                            System.IO.File.WriteAllBytes(this.path, System.IO.File.ReadAllBytes(model.path));
                        }
                        this.initialHash = NGSyncFoldersWindow.TryGetCachedHash(path);
                        this.slaveState  = SlaveState.Exist;

                        InternalNGDebug.InternalLogFormat("Sync \"{0}\": Created.", this.path);
                    }
                    else if (action == Action.Change || force == true)
                    {
                        Directory.CreateDirectory(Path.GetDirectoryName(this.path));
                        System.IO.File.WriteAllBytes(this.path, System.IO.File.ReadAllBytes(model.path));
                        this.initialHash = NGSyncFoldersWindow.TryGetCachedHash(path);

                        InternalNGDebug.InternalLogFormat("Sync \"{0}\": Restored.", this.path);
                    }

                    this.initialState = InitialState.Origin;

                    this.InvalidateHeight();
                }
                catch (Exception ex)
                {
                    this.actionException = ex;
                    if (model != null)
                    {
                        InternalNGDebug.LogException("Syncing file " + this.path + " with " + model.path + " failed. (" + Enum.GetName(typeof(Action), action) + ")", ex);
                    }
                    else
                    {
                        InternalNGDebug.LogException("Syncing file " + this.path + " failed. (" + Enum.GetName(typeof(Action), action) + ")", ex);
                    }
                }
            }
        }
コード例 #4
0
        public File     Generate(string path)
        {
            this.InvalidateHeight();

            if (this.path == path)
            {
                string hash = NGSyncFoldersWindow.TryGetCachedHash(path);

                if (this.isMaster == true)
                {
                    if (hash == this.initialHash)
                    {
                        this.masterState = MasterState.Same;
                    }
                    else
                    {
                        this.masterState = MasterState.Altered;
                    }
                }
                else
                {
                    this.initialHash = hash;
                    this.slaveState  = SlaveState.Exist;
                }

                this.targetHash = hash;

                return(this);
            }

            string oldPath = path.Substring(0, this.path.Length + 1);

            string[] newPath = path.Substring(this.path.Length + 1).Split(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
            File     parent  = this;

            path = this.path;

            for (int i = 0; i < newPath.Length; i++)
            {
                oldPath = Path.Combine(oldPath, newPath[i]);
                path    = Path.Combine(path, newPath[i]);

                File child = null;

                for (int j = 0; j < parent.children.Count; j++)
                {
                    if (parent.children[j].path == path)
                    {
                        child = parent.children[j];
                        break;
                    }
                }

                if (child == null)
                {
                    InternalNGDebug.InternalLogFormat("Generating {0} in {1} ({2})", path, parent, oldPath);
                    if (this.isMaster == true)
                    {
                        child = new File(parent, path, Path.GetFileName(path), true, Directory.Exists(oldPath), InitialState.New, MasterState.Created);
                    }
                    else
                    {
                        child = new File(parent, path, Path.GetFileName(path), false, Directory.Exists(oldPath), InitialState.New, System.IO.File.Exists(path) ? SlaveState.Exist : SlaveState.NonExist);
                    }
                    parent.children.Add(child);
                    parent.children.Sort((a, b) => string.CompareOrdinal(a.path, b.path));
                }

                parent = child;
            }

            return(parent);
        }