ExportWorldProject ( plan$,directory$ )

Parameters

plan$ - path of a JSON export plan

directory$ - the new folder to export into; it must not exist yet, but its parent folder must

Description

Copies a project's saved documents, and everything they depend on, into a new folder.

Use it to get a game ready to ship: the export holds exactly the files your worlds and resources need, in the same folder layout, and nothing else. The plan says what to export:

{
    "format": "blitz3d.resource-export", "version": 1,
    "projectRoot": ".",
    "roots": ["scene.world", "sounds/code-only.sound"],
    "files": ["Game.exe", "LICENSE.txt"]
}

projectRoot is relative to the plan, and every other path is relative to the project root. roots lists the documents to export, including any your code loads by name - a file name built in code cannot be found by reading a world. files adds anything else the game needs, such as its executable and licence. A dependencies object can list the extra files of models such as .x and .3ds, whose textures cannot be found automatically.

It exports what is saved on disk, so save first. No source file is changed, and an existing folder is never replaced. Missing dependencies, paths outside the project root and names that clash stop the export. The export's documents are checked from their new place; your game is not compiled, and its media is not played.

Returns a JSON manifest listing every exported file with its hash - also saved as export-manifest.json in the folder - or 0 with the reason in WorldDocumentError(). The handle is yours: free it with FreeJson. From the command line, b3dworldc --export does the same, as does the World Editor's File > Export Project.

For the full story - the edit protocol, Save All, recovery, export plans and limits - see the Saving and exporting resource projects guide.


See also: SaveWorldDocumentSet, RecoverWorldDocumentSet, WorldDocumentError, FreeJson.

Example

; SaveWorldDocumentSet Example
; ----------------------------

Print "This example saves a tiny project into demo_project next to itself,"
Print "exports a copy into demo_export, then deletes both folders."
Print "Press Y to continue, any other key to skip."
WaitKey
If KeyDown(21)

    ; A world that uses a paint material: the project's first two files
    CreateDir "demo_project"
    paint_text$="{'format':'blitz3d.material','version':1,'id':'paint','assets':[],"
    paint_text=Replace(paint_text+"'definition':{'model':'classic','color':[0.2,0.5,0.9,1]}}","'",Chr(34))
    scene_text$="{'format':'blitz3d.world','version':1,'id':'scene','entities':[],"
    scene_text=scene_text+"'assets':[{'id':'paint','kind':'material','source':'paint.material','loading':'required'}]}"
    WriteTextAtomic "demo_project/paint.material",paint_text
    WriteTextAtomic "demo_project/scene.world",Replace(scene_text,"'",Chr(34))
    world=LoadWorldDocument("demo_project/scene.world")
    paint=LoadResourceDocument("demo_project/paint.material")

    ; Repaint the material in memory: a draft that is not saved yet
    ReplaceResourceDocumentSource paint,Replace(paint_text,"0.2,0.5,0.9","0.9,0.3,0.1"),ResourceDocumentRevision(paint)
    Print ""
    Print "Before saving, the paint draft is dirty: "+ResourceDocumentDirty(paint)

    ; Save the world and the paint together, as one checked set
    text$="[{'document':"+paint+",'revision':"+ResourceDocumentRevision(paint)+"}]"
    drafts=ParseJson(Replace(text,"'",Chr(34)))
    If Not SaveWorldDocumentSet(world,drafts,WorldDocumentRevision(world),"demo_project") Then RuntimeError WorldDocumentError()
    FreeJson drafts
    Print "SaveWorldDocumentSet returned True; the paint is dirty: "+ResourceDocumentDirty(paint)

    ; A save that was cut short leaves a journal folder to recover from
    If FileType("demo_project/.b3d-save-journal")=2 Then
        If Not RecoverWorldDocumentSet("demo_project") Then RuntimeError WorldDocumentError()
        Print "Recovered an interrupted save"
    Else
        Print "No save was interrupted, so RecoverWorldDocumentSet has nothing to do"
    EndIf

    ; Export the world and everything it needs into a new folder
    text="{'format':'blitz3d.resource-export','version':1,'projectRoot':'.','roots':['scene.world']}"
    WriteTextAtomic "demo_project/export.json",Replace(text,"'",Chr(34))
    manifest=ExportWorldProject("demo_project/export.json","demo_export")
    If manifest=0 Then RuntimeError WorldDocumentError()
    files=JsonObjectGet(manifest,"files")
    Print "ExportWorldProject copied "+JsonCount(files)+" files into demo_export"
    FreeJson files
    FreeJson manifest

    FreeResourceDocument paint
    FreeWorldDocument world
    RemoveDemoFolder("demo_export")
    RemoveDemoFolder("demo_project")
    Print "Demo folders deleted"

Else
    Print "Skipped"
EndIf

Print ""
Print "Press any key to close the example"
WaitKey

End

; Delete a folder this example made, with the files in it
Function RemoveDemoFolder(folder$)
    listing=ReadDir(folder)
    Repeat
        file$=NextFile(listing)
        If FileType(folder+"/"+file)=1 Then DeleteFile folder+"/"+file
    Until file=""
    CloseDir listing
    DeleteDir folder
End Function

Index