// Run through the previously found books, and retrieve these. Any // bad files are ignored, and these will be recreated if the books are // found on the explicit file search. private IEnumerator searchCache(float progressProp) { string [] cache = BookPropertySet.getPropertyCacheFiles(); foreach (string book in cache) { BookPropertySet bp = new BookPropertySet(); bool loaded = false; try { bp.RawLoad(book); loaded = true; } catch (Exception) { // bad file, ignore. } if (loaded && (File.Exists(bp.filename))) { yield return(registerBook(bp.filename)); } progress += progressProp * (1.0f / cache.Length); updateProgress(); } }
public bool addBook(BookPropertySet bp) { if (freeSlots > 0) { int slot; for (slot = 0; slot < bookSlots.Length; slot++) { if (bookSlots[slot] == null) { break; } } if (slot < bookSlots.Length) { bookSlots[slot] = bp; freeSlots--; persist(); placeBook(bp, slot); return(true); } } return(false); }
private void doLoading(object obp) { BookPropertySet bp = (BookPropertySet)obp; int rx = 1; int ry = 1; Debug.Log("Try to open : " + bp.filename); bookHandle = bookEngine.BEIDocViewCreate(bp.filename); bookEngine.BEILoadDocument(bookHandle, bp.filename, rx, ry); bp.title = Marshal.PtrToStringAnsi(bookEngine.BEIGetTitle(bookHandle)); if (bp.title == null) { bp.title = ""; } bp.author = Marshal.PtrToStringAnsi(bookEngine.BEIGetAuthors(bookHandle)); if (bp.author == null) { bp.author = "No author"; } if (bp.title == "") { bp.title = Path.GetFileNameWithoutExtension(bp.filename); } Debug.Log("Found book " + bp.title + " " + bp.author + " " + bp.filename); loading = false; // Each book is given a colour to distinguish it. Similar books (same author) get similar colours. bp.colour[0] = 0.75f + 0.25f * (float)bp.filename.GetHashCode() / Int32.MaxValue; bp.colour[1] = 0.75f + 0.25f * (float)bp.title.GetHashCode() / Int32.MaxValue; bp.colour[2] = 0.75f + 0.25f * (float)bp.author.GetHashCode() / Int32.MaxValue; }
// Open the ebook, so its metadata can be accessed. private IEnumerator getBookProperties(BookPropertySet bp) { bool retrieved = false; if (BookPropertySet.haveRecord(bp.filename)) { try { bp.Load(bp.filename); retrieved = true; } catch (Exception) { Debug.Log("Bad file " + bp.filename); } } if (!retrieved) { loading = true; Thread t = new Thread(new ParameterizedThreadStart(doLoading)); t.Start(bp); while (loading) { yield return(new WaitForSeconds(0.1f)); } // save record. bp.Save(); } }
// Create a book from the filename to the ebook. private IEnumerator loadBookInternal(string bookFileName, BookPropertySet props) { setInformation("Loading"); yield return(null); initializeTextures(); setInformation("Loading."); yield return(null); // Try to load the book in a separate thread. loadingName = bookFileName; rx = directRenderTexture.width; ry = directRenderTexture.height; loading = true; Thread t = new Thread(new ThreadStart(doLoading)); t.Start(); while (loading) { yield return(new WaitForSeconds(0.1f)); print("Waiting"); } setInformation("Loading.."); yield return(null); // Retrieve the title and author settings. string title = Marshal.PtrToStringAnsi(bookEngine.BEIGetTitle(bookHandle));; string author = Marshal.PtrToStringAnsi(bookEngine.BEIGetAuthors(bookHandle)); Debug.Log("Author " + author + " title: " + title); // Retrieve the cover image. This involves render operations so // cannot take place in its own thread. retrieveCoverMaterial(frontCover); setInformation("Loading..."); yield return(null); GetComponent <BookProperties> ().props = props; //bookProperties = props; // Change page to force initial page drawing. currentPage = props.currentPage; // changePage (currentPage); // pageTurnComplete (); fontSize = props.fontSize; yield return(updateFont()); fontSize = bookEngine.BEIGetFontSize(bookHandle); bookLoaded = true; setInformation(""); stateChanged(); }
public static byte[] Serialize (object bpo) { BinaryFormatter bf = new BinaryFormatter (); BookPropertySet bp = (BookPropertySet) bpo; MemoryStream ms = new MemoryStream (); bf.Serialize (ms, bp.filename); bf.Serialize (ms, bp.author); bf.Serialize (ms, bp.title); bf.Serialize (ms, bp.colour); bf.Serialize (ms, bp.currentPage); bf.Serialize (ms, bp.fontSize); return ms.GetBuffer (); }
// A response when clicking on the book itself. Replace the book with an full book object. public void getBook(ControlInput controller, ControlInput.ControllerDescription controllerObject, GameObject button, GameObject avatar, bool initialize = false) { if (!initialize) { BookPropertySet props = button.GetComponentInParent <BookProperties> ().props; string fname = props.filename; GameObject book = Instantiate(fullBookTemplate); BookManager bookManager = book.GetComponent <BookManager> (); // StartCoroutine (bookManager.loadBook (fname, props)); bookManager.loadBook(fname, props); Debug.Log("Loading book " + fname); book.GetComponent <BookMenuInteraction> ().pickupBook(controller, controllerObject, button, avatar); Destroy(button.transform.parent.gameObject); } }
// A response when clicking on the book itself. Replace the book with an full book object. public void downloadBook(ControlInput controller, ControlInput.ControllerDescription controllerObject, GameObject button, GameObject avatar, bool initialize = false) { if (!initialize) { BookPropertySet props = bProps.props; string fname = props.filename; Debug.Log("Downloading book" + fname); StartCoroutine(downloadProcess()); // GetComponent <PhotonView> ().RPC ("retrieveBook", RpcTarget.All, 0, 1000); // GameObject book = Instantiate (fullBookTemplate); // BookManager bookManager = book.GetComponent <BookManager> (); // // StartCoroutine (bookManager.loadBook (fname, props)); // bookManager.loadBook (fname, props); // Debug.Log ("Loading book " + fname); // book.GetComponent <BookMenuInteraction> ().pickupBook (controller, controllerObject, button, avatar); // Destroy (button.transform.parent.gameObject); } }
IEnumerator downloadProcess() { localData = null; segments = null; nextSegmentStart = 0; segmentCount = -1; while ((segments == null) || (segmentCount < segments.Length)) { if (segments != null) { Debug.Log("Requesting " + nextSegmentStart + " / " + segmentCount + " " + segments.Length); } else { Debug.Log("Requesting " + nextSegmentStart + " / " + segmentCount); } receivedResponse = false; // // GetComponent <PhotonView> ().RPC ("retrieveBook", RpcTarget.All, nextSegmentStart, packetLength); Debug.Log("Sending req to " + GetComponent <PhotonView> ().Owner); GetComponent <PhotonView> ().RPC("retrieveBook", GetComponent <PhotonView> ().Owner, nextSegmentStart, packetLength); Coroutine c = StartCoroutine(timeoutCoroutine(retransmitTimeout)); yield return(new WaitUntil(() => receivedResponse)); StopCoroutine(c); } File.WriteAllBytes(getFilePath(), localData); GameObject book = Instantiate(bookTemplate, new Vector3(-3, 2, 2), Quaternion.identity); BookPropertySet bps = new BookPropertySet(); bps.filename = getFilePath(); book.GetComponent <BookProperties> ().props = bps; BookManager bookManager = book.GetComponent <BookManager> (); // book.GetComponent <BookPlaceholdMenu> ().pickupBook (controller, controllerObject, button, avatar); Destroy(this.gameObject); Debug.Log("All Done"); }
public void addBook(string bookIdentifier, GameObject bookMarkerPrefab) { books.Add(bookIdentifier); if (bookMarkerPrefab != null) { BookPropertySet bp = new BookPropertySet(); bp.Load(bookIdentifier); GameObject bookObject = UnityEngine.Object.Instantiate(bookMarkerPrefab, new Vector3(2, 2, 2), Quaternion.identity); bookObject.GetComponent <BookProperties> ().props = bp; bookObject.transform.localRotation = Quaternion.AngleAxis(90.0f, Vector3.right); // bookObject.transform.localScale = new Vector3 (0.98f * bookThickness, 0.6f * trolleyDepth, trolleyDepth); bookObject.transform.Find("CheapBook").GetComponent <MeshRenderer> ().material.color = new Color(bp.colour[0], bp.colour[1], bp.colour[2]); // seems to fix a unity bug that prevents some colliders from being spotted. bookObject.GetComponent <BoxCollider> ().enabled = false; bookObject.GetComponent <BoxCollider> ().enabled = true; } Debug.Log("Added " + bookIdentifier); }
public static BookPropertySet Deserialize (byte[] data) { MemoryStream ms = new MemoryStream (data); BookPropertySet bp = new BookPropertySet (); try { BinaryFormatter bf = new BinaryFormatter (); bp.filename = (string) bf.Deserialize (ms); bp.author = (string) bf.Deserialize (ms); bp.title = (string) bf.Deserialize (ms); bp.colour = (float []) bf.Deserialize (ms); bp.currentPage = (int) bf.Deserialize (ms); bp.fontSize = (int) bf.Deserialize (ms); } catch (Exception) { throw; } return bp; }
public void restore() { FileStream file = null; try { BinaryFormatter bf = new BinaryFormatter(); file = File.Open(getFilePath(), FileMode.Open); int maxBooks = (int)bf.Deserialize(file); bookSlots = new BookPropertySet [maxBooks]; freeSlots = maxBooks; for (int i = 0; i < bookSlots.Length; i++) { string fn = (string)bf.Deserialize(file); if (fn.Length > 0) { bookSlots[i] = new BookPropertySet(); bookSlots[i].Load(fn); freeSlots--; placeBook(bookSlots[i], i); } else { bookSlots[i] = null; } } file.Close(); } catch (Exception) { if (file != null) { file.Close(); } throw; } }
// Internal method to ensure the given slot has a book corresponding to // the specified property set. private void placeBook(BookPropertySet bp, int slot) { float bookOffset = slot * bookThickness; // Make a spine label with author and title. string bookSpine = bp.author + "\n" + bp.title; float x = 0.0f; float y = 0.9f; float z = 0.0f; // Create the book. GameObject bookObject = addMenuOption(bookSpine, new Vector3(x + bookOffset, y, z), (controller, controllerObject, button, avatar, initialize) => selectBook(slot, controller, controllerObject, button, avatar, initialize), moveResponse); bookObject.GetComponent <BookProperties> ().props = bp; bookObject.transform.localRotation = Quaternion.AngleAxis(90.0f, Vector3.right); bookObject.transform.localScale = new Vector3(0.98f * bookThickness, 0.6f * trolleyDepth, trolleyDepth); bookObject.transform.Find("CheapBook").GetComponent <MeshRenderer> ().material.color = new Color(bp.colour[0], bp.colour[1], bp.colour[2]); // seems to fix a unity bug that prevents some colliders from being spotted. bookObject.GetComponent <BoxCollider> ().enabled = false; bookObject.GetComponent <BoxCollider> ().enabled = true; }
public void loadBook(string bookFileName, BookPropertySet props) { StartCoroutine(loadBookInternal(bookFileName, props)); }
// Add a new book to the library. public IEnumerator addBook(string book) { // Find the spot for the next book on the shelves, float bookWidth = bookThickness; float shelfSeparation = (bookcaseHeight - floorOffset - plankThickness) / numberShelves; float bookOffset = offsetinShelf; float x = rack * (shelfLength + plankThickness) - (0.5f * shelfLength * setsPerRow); float y = floorOffset + shelf * shelfSeparation; float z = stack * rowGap; if (book != null) { // Get the properties. BookPropertySet bp = new BookPropertySet(); bp.filename = book; yield return(getBookProperties(bp)); // Make a spine label with author and title. string bookSpine = bp.author + "\n" + bp.title; // Create the book. GameObject bookObject = addMenuOption(bookSpine, new Vector3(x + plankThickness + bookOffset, y + plankThickness, z), selectBook, moveResponse); bookObject.GetComponent <BookProperties> ().props = bp; bookObject.transform.localScale = new Vector3(0.98f * bookWidth, 0.8f * shelfSeparation, 0.95f * bookcaseDepth); bookObject.transform.Find("CheapBook").GetComponent <MeshRenderer> ().material.color = new Color(bp.colour[0], bp.colour[1], bp.colour[2]); // seems to fix a unity bug that prevents some colliders from being spotted. bookObject.GetComponent <BoxCollider> ().enabled = false; bookObject.GetComponent <BoxCollider> ().enabled = true; } // Make space for the book. if (z + endwallSectionSize > endwallPosition) { createSection(new Vector3(0, 0, endwallPosition)); endWall.transform.localPosition = new Vector3(0, 0, endwallPosition); endwallPosition += endwallSectionSize; } if (spareCapacity <= 0) { // Extend the shelves. if (rack == 0) { createUpright(new Vector3(x, 0, z)); } for (int i = 0; i < numberShelves + 1; i++) { y = floorOffset + i * ((bookcaseHeight - floorOffset - plankThickness) / numberShelves); createShelf(new Vector3(x + plankThickness, y, z)); } createUpright(new Vector3(x + shelfLength + plankThickness, 0, z)); spareCapacity += shelfLength * numberShelves; createTeleport(new Vector3(x + 0.5f * shelfLength, 0.1f, z - 0.5f * rowGap), Quaternion.AngleAxis(0, Vector3.up)); if (rack == 0) { createTeleport(new Vector3(x - 1.5f * shelfLength, 0.1f, z), Quaternion.AngleAxis(90, Vector3.up)); createTeleport(new Vector3(x - 1.5f * shelfLength, 0.1f, z - 0.5f * rowGap), Quaternion.AngleAxis(90, Vector3.up)); } if (rack == setsPerRow - 1) { createTeleport(new Vector3(x + 2.5f * shelfLength, 0.1f, z), Quaternion.AngleAxis(-90, Vector3.up)); createTeleport(new Vector3(x + 2.5f * shelfLength, 0.1f, z - 0.5f * rowGap), Quaternion.AngleAxis(-90, Vector3.up)); } } if (book != null) { advanceShelfCounter(bookWidth); } }