Example #1
0
    public static void ImportClouds()
    {
        if (EditorUtility.DisplayDialog("Import clouds",
                                        string.Format(
                                        "Folders are setup in Resources/ExplodedPrefs asset\n\n" +
                                        "Incoming: {0}\n" +
                                        "Imported: {1}", Prefs.IncomingPath, Prefs.ImportedPath ),
                                        "Import",
                                        "Cancel")) {

            // get a list of incoming .cloud files
            string[] clouds = Directory.GetFiles(Prefs.IncomingPath, "*.cloud");
            // see if the list isn't empty
            if (clouds.Length == 0) {
                Debug.LogWarning(string.Format("No cloud files at incoming path: {0}", Prefs.IncomingPath));
                return;
            }

            Progressor prog = new Progressor("Importing clouds");

            using(new AssetEditBatch() ) {
                foreach(string cloud_path in prog.Iterate(clouds )) {
                    // derive .prefab / .bin paths from .cloud path
                    string bin_path = Prefs.IncomingBin(cloud_path);
                    string prefab_path = Prefs.ImportedCloudPrefab(cloud_path);

                    // Sanity check: there should be a corresponding .bin next to the cloud
                    if (!File.Exists(bin_path)) {
                        Debug.LogError(string.Format("No .bin file found for '{0}'", cloud_path));
                        continue;
                    }

                    // Safety: don't overwrite prefabs
                    string[] sentinels = { prefab_path, Prefs.ImportedBin(cloud_path), Prefs.ImportedCloud(cloud_path)};
                    bool hitSentinel = false;
                    foreach(string sentinel in sentinels) {
                        if (File.Exists( sentinel )) {
                            Debug.LogWarning(string.Format("'{0}' is in the way when importing '{1}'", sentinel, cloud_path));
                            hitSentinel = true;
                        }
                    }
                    if (hitSentinel)
                        continue;

                    // ready to import
                    CloudImporter importer = new CloudImporter(prog.Sub());
                    importer.ImportCloud(cloud_path, bin_path, prefab_path);
                }
            }
        }
    }
Example #2
0
 public void DistinctProgressor()
 {
     var source = new Progressor<int>(new[] { 0, 0, 1, 1, 2, 0, 1, 3, 4, 4, 4, 4, 5, 5, 5, 0, 1, 3 });
     var progresor = Progressor<int>.CreateDistinct(source);
     int indexA = 0;
     int indexB = 0;
     progresor.SubscribeAction
     (
         value =>
         {
             Assert.AreEqual(value, indexB);
             indexB++;
         }
     );
     int item;
     while (progresor.TryTake(out item))
     {
         Assert.AreEqual(item, indexA);
         indexA++;
     }
     Assert.AreEqual(6, indexA);
     Assert.AreEqual(indexA, indexB);
 }
Example #3
0
 public void CreatedFilteredConverted()
 {
     var source = new Progressor<int>(new[] { 0, 8, 1, 8, 2, 3, 4, 8, 5 });
     var progresor = Progressor<string>.CreatedFilteredConverted(source, input => input != 8, input => input.ToString(CultureInfo.InvariantCulture));
     int indexA = 0;
     int indexB = 0;
     progresor.SubscribeAction
     (
         value =>
         {
             Assert.AreEqual(value, indexB.ToString(CultureInfo.InvariantCulture));
             indexB++;
         }
     );
     string item;
     while (progresor.TryTake(out item))
     {
         Assert.AreEqual(item, indexA.ToString(CultureInfo.InvariantCulture));
         indexA++;
     }
     Assert.AreEqual(6, indexA);
     Assert.AreEqual(indexA, indexB);
 }
Example #4
0
 public void EnumerableProgressor()
 {
     var source = new[] { 0, 1, 2, 3, 4, 5 };
     var progresor = new Progressor<int>(source);
     int indexA = 0;
     int indexB = 0;
     progresor.SubscribeAction
     (
         value =>
         {
             Assert.AreEqual(value, indexB);
             indexB++;
         }
     );
     int item;
     while (progresor.TryTake(out item))
     {
         Assert.AreEqual(item, indexA);
         indexA++;
     }
     Assert.AreEqual(6, indexA);
     Assert.AreEqual(indexA, indexB);
 }
Example #5
0
 public void TryTakeProgressor()
 {
     var source = new Queue<int>();
     source.Enqueue(0);
     source.Enqueue(1);
     source.Enqueue(2);
     source.Enqueue(3);
     source.Enqueue(4);
     source.Enqueue(5);
     var progresor = new Progressor<int>
     (
         (out int value) =>
         {
             try
             {
                 value = source.Dequeue();
                 return true;
             }
             catch (Exception)
             {
                 value = 0;
                 return false;
             }
         },
         false
     );
     int indexA = 0;
     int indexB = 0;
     progresor.SubscribeAction
     (
         value =>
         {
             Assert.AreEqual(value, indexB);
             indexB++;
         }
     );
     int item;
     while (progresor.TryTake(out item))
     {
         Assert.AreEqual(item, indexA);
         indexA++;
     }
     Assert.AreEqual(6, indexA);
     Assert.AreEqual(indexA, indexB);
 }
Example #6
0
 public void ThreadedUseWithPreface()
 {
     var source = new Progressor<int>( new List<int> {7, 7, 7, 7, 7}, new Progressor<int>(new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 })).AsEnumerable();
     var handle = new ManualResetEvent(false);
     int[] count = { 0, 0, 0 };
     var work = new WaitCallback
         (
             _ =>
             {
                 Interlocked.Increment(ref count[0]);
                 handle.WaitOne();
                 foreach (var item in source)
                 {
                     GC.KeepAlive(item);
                     Interlocked.Increment(ref count[2]);
                 }
                 Interlocked.Increment(ref count[1]);
             }
         );
     ThreadPool.QueueUserWorkItem(work);
     ThreadPool.QueueUserWorkItem(work);
     while (Thread.VolatileRead(ref count[0]) != 2)
     {
         Thread.Sleep(0);
     }
     handle.Set();
     while (Thread.VolatileRead(ref count[1]) != 2)
     {
         Thread.Sleep(0);
     }
     Assert.AreEqual(15, Thread.VolatileRead(ref count[2]));
     handle.Close();
 }
Example #7
0
 public void FilteredProgressor()
 {
     var source = new Progressor<int>(new[] { 0, 8, 1, 8, 2, 3, 4, 8, 5 });
     var progresor = Progressor<int>.CreatedFiltered(source, input => input != 8);
     int indexA = 0;
     int indexB = 0;
     progresor.SubscribeAction
     (
         value =>
         {
             Assert.AreEqual(value, indexB);
             indexB++;
         }
     );
     int item;
     while (progresor.TryTake(out item))
     {
         Assert.AreEqual(item, indexA);
         indexA++;
     }
     Assert.AreEqual(6, indexA);
     Assert.AreEqual(indexA, indexB);
 }
Example #8
0
 public void Progressor_ThreadedUse()
 {
     var source = new Progressor<int>(new List<int>
     {
         0,
         1,
         2,
         3,
         4,
         5,
         6,
         7,
         8,
         9
     }).AsEnumerable();
     var handle = new ManualResetEvent(false);
     int[] count = { 0, 0, 0 };
     Action work = () =>
     {
         Interlocked.Increment(ref count[0]);
         handle.WaitOne();
         foreach (var item in source)
         {
             GC.KeepAlive(item);
             Interlocked.Increment(ref count[2]);
         }
         Interlocked.Increment(ref count[1]);
     };
     Task.Factory.StartNew(work);
     Task.Factory.StartNew(work);
     while (Thread.VolatileRead(ref count[0]) != 2)
     {
         Thread.Sleep(0);
     }
     handle.Set();
     while (Thread.VolatileRead(ref count[1]) != 2)
     {
         Thread.Sleep(0);
     }
     Assert.AreEqual(10, Thread.VolatileRead(ref count[2]));
     handle.Close();
 }
Example #9
0
    void CutToBoxes(GameObject cloud_go)
    {
        // using linked list so we can change order on the fly
        ImportedCloud iCloud = cloud_go.GetComponent<ImportedCloud>();
        slices = new LinkedList<Slice>( iCloud.slices );

        maxTodoCount = cutBoxes.Count * Prefs.MaxCompactSize;
        done = 0;
        portionCount = 0;

        Progressor prog = new Progressor ("Cutting " + cloud_go.name + " according to boxes");

        // open the original file for reading
        origReader = new CloudStream.Reader( new FileStream( Prefs.ImportedBin(cloud_go.name) , FileMode.Open, FileAccess.Read));
        try {
            // since we get rid of full boxes and exhausted slices ...
            while( cutBoxHelpers.Count > 0 && slices.Count > 0 ) {
                // iterate over remaining slices
                LinkedListNode<Slice> slice_iter = slices.First;
                do {
                    // deal with this once slice ...
                    SortSlicePortion(slice_iter, cutBoxHelpers, shadowBoxHelpers);
                    prog.Progress( (float)done/(float)maxTodoCount, "Sorting...");
                } while (cutBoxHelpers.Count > 0 && (slice_iter = slice_iter.Next) != null);
                portionCount++;
            }
            // close remaining boxes
            foreach (BoxHelper box in cutBoxHelpers)
                box.Finish ();
            foreach (BoxHelper box in shadowBoxHelpers)
                box.Finish ();

        } finally {
            prog.Done();
        }
    }
Example #10
0
 CloudImporter(Progressor _prog)
 {
     prog = _prog;
     meshConv = new CloudMeshConvertor( Prefs.OrigPreviewSize );
 }
Example #11
0
 /// <summary> Set the Progressor reference that will get updates when this SceneLoader loads a scene </summary>
 /// <param name="progressor"> The Progressor that will get updates when this SceneLoader loads a scene </param>
 public SceneLoader SetProgressor(Progressor progressor)
 {
     Progressor = progressor;
     return(this);
 }
Example #12
0
    public void Shuffle(FileStream stream, Progressor prog)
    {
        int byteCount = pointCount * CloudStream.pointRecSize;
        byte[] wholeThing = new byte[byteCount];

        Progressor subProg = prog.Sub(0f, 0.1f);
        subProg.Progress(0.01f, "Reading into memory...");
        stream.Seek(0, SeekOrigin.Begin);
        stream.Read( wholeThing, 0, byteCount );
        subProg.Progress(1f, "Reading into memory... done");

        byte[] tmp = new byte[CloudStream.pointRecSize];

        subProg = prog.Sub(0.1f, 0.9f);
        ShuffleUtility.WithSwap(pointCount, (i, j) =>
        {
            /*
             * This is the fastest way I found to swap 16-byte long chunks in memory (tried MemoryStream and
             * byte-by-byte swap loop).
             */
            System.Buffer.BlockCopy(wholeThing, i * CloudStream.pointRecSize, tmp, 0, CloudStream.pointRecSize);
            System.Buffer.BlockCopy(wholeThing, j * CloudStream.pointRecSize, wholeThing, i * CloudStream.pointRecSize, CloudStream.pointRecSize);
            System.Buffer.BlockCopy(tmp, 0, wholeThing, j * CloudStream.pointRecSize, CloudStream.pointRecSize);
            // 'i' runs backwards from pointCount-1 to 0
            subProg.Progress((float)(pointCount - i) / pointCount, "Shuffling '{0}' in memory. ETA: {eta}", name);
        });

        subProg = prog.Sub(0.9f, 1f);
        subProg.Progress(0.01f, "Writing to disk...");
        stream.Seek(0, SeekOrigin.Begin);
        stream.Write( wholeThing, 0, byteCount );
        subProg.Progress(1f, "Writing to disk... done");
    }