package scratchlib.objects;

import java.io.IOException;

import scratchlib.project.ScratchProject;
import scratchlib.writer.ScratchOutputStream;


/**
 * Represents a single object inside a Scratch project. Can be any form of data,
 * from simple constants like true/false/nil, to numbers, to collections, and
 * finally to arbitrarily complex user-class types.
 */
public class ScratchObject
{
    /**
     * The singleton object representing a value of {@code null} ("nil").
     */
    public static final ScratchObject NIL = new ScratchObject(1);

    private final int classID;

    /**
     * @param classID The ID of the class this object belongs to.
     */
    public ScratchObject(int classID)
    {
        this.classID = classID;
    }

    /**
     * @return The ID of the class this object belongs to.
     */
    public int getClassID()
    {
        return classID;
    }

    /**
     * Inserts all reference fields of this object into the given reference
     * table. Only inserts child fields, not this element.
     * 
     * @param ref The reference table the fields shall be inserted into.
     * @param project The project this object belongs to, for version info.
     */
    public void createReferences(ScratchReferenceTable ref,
            ScratchProject project)
    {
    }

    /**
     * Replaces all unresolved reference fields this instance has by looking
     * them up in the given reference table.
     * 
     * @param ref The reference table to use for field lookup.
     */
    public void resolveReferences(ScratchReferenceTable ref)
    {
    }

    /**
     * Writes this object to the given {@link ScratchOutputStream}, using the
     * already populated {@link ScratchReferenceTable} for writing out reference
     * fields.
     * 
     * @param out The stream to write to.
     * @param ref The populated reference table.
     * @param project The project this object belongs to, for version info.
     * @throws IOException
     * 
     * @see #createReferences(ScratchReferenceTable, ScratchProject)
     */
    public void writeTo(ScratchOutputStream out, ScratchReferenceTable ref,
            ScratchProject project) throws IOException
    {
        out.write(classID);
    }
}

@@ -93,16 +93,61 @@
 roject;%0A
+import scratchlib.reader.ScratchInputStream;%0A
 import s
@@ -1810,32 +1810,125 @@
 fields.%0A     * %0A
+     * %3Cp%3E%0A     * Subclasses MUST override this if they store any (additional) data.%0A     * %0A
      * @param ou
@@ -2360,11 +2360,638 @@
 );%0A    %7D
+%0A%0A    /**%0A     * Initializes this object's fields by reading from the given stream.%0A     * %0A     * %3Cp%3E%0A     * Subclasses MUST override this if they store any (additional) data.%0A     * %0A     * @param id This object's class ID.%0A     * @param in The input stream to read from.%0A     * @param project The project this object belongs to, for version info.%0A     * @throws IOException%0A     */%0A    public void readFrom(int id, ScratchInputStream in, ScratchProject project)%0A            throws IOException%0A    %7B%0A        if (id != classID) %7B%0A            throw new IOException(%22illegal ID %22 + id + %22, expected %22 + classID);%0A        %7D%0A    %7D
 %0A%7D%0A

Add readFrom() method to ScratchObject