Esempio n. 1
0
        public static string    TryGetCachedHash(string file)
        {
            if (NGSyncFoldersWindow.cachedHashes == null)
            {
                NGSyncFoldersWindow.LoadHashesCache();
            }

            string[] values;

            if (NGSyncFoldersWindow.cachedHashes.TryGetValue(file, out values) == true)
            {
                string currentWriteTime = System.IO.File.GetLastWriteTime(file).Ticks.ToString();

                if (currentWriteTime != values[0])
                {
                    values[0] = currentWriteTime;
                    values[1] = NGSyncFoldersWindow.ProcessHash(file);
                }

                return(values[1]);
            }

            string hash = NGSyncFoldersWindow.ProcessHash(file);

            NGSyncFoldersWindow.cachedHashes.Add(file, new string[] { System.IO.File.GetLastWriteTime(file).Ticks.ToString(), hash });
            return(hash);
        }
Esempio n. 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;
 }
Esempio n. 3
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;
 }
Esempio n. 4
0
        private void    Scan()
        {
            try
            {
                if (this.profile.useCache == false)
                {
                    if (NGSyncFoldersWindow.cachedHashes == null)
                    {
                        NGSyncFoldersWindow.cachedHashes = new Dictionary <string, string[]>();
                    }
                    else
                    {
                        NGSyncFoldersWindow.cachedHashes.Clear();
                    }
                }
                else
                {
                    if (EditorUtility.DisplayCancelableProgressBar(NGSyncFoldersWindow.Title, "Loading cache...", 0F) == true)
                    {
                        return;
                    }

                    NGSyncFoldersWindow.cachedHashes = null;
                    NGSyncFoldersWindow.LoadHashesCache();
                }

                float countActiveSlaves = 2F;                   // Cache + master

                for (int i = 0; i < this.profile.slaves.Count; i++)
                {
                    if (this.profile.slaves[i].active == true)
                    {
                        ++countActiveSlaves;
                    }
                }

                NGSyncFoldersWindow.progress = 1F / countActiveSlaves;

                this.profile.master.Scan(/*this.mode == ButtonMode.ScanAndWatch, */ this.profile.relativePath, this.profile.filters);
                for (int i = 0; i < this.profile.slaves.Count; i++)
                {
                    if (this.profile.slaves[i].active == true)
                    {
                        NGSyncFoldersWindow.progress = (i + 2F) / countActiveSlaves;
                        NGSyncFoldersWindow.slave    = i + 1;

                        this.profile.slaves[i].ScanDiff(/*this.mode == ButtonMode.ScanAndWatch, */ this.profile.master, this.profile.relativePath, this.profile.filters);
                    }
                    //else
                    //	this.profile.slaves[i].Dispose();
                }

                if (this.profile.useCache == true)
                {
                    NGSyncFoldersWindow.SaveCachedHashes();
                    this.UpdateCacheFileSize();
                }
            }
            catch (AbortScanException)
            {
                this.profile.master.Clear();

                for (int i = 0; i < this.profile.slaves.Count; i++)
                {
                    this.profile.slaves[i].Clear();
                }
            }
            finally
            {
                EditorUtility.ClearProgressBar();
            }
        }
Esempio n. 5
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);
                    }
                }
            }
        }
Esempio n. 6
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);
        }