Changeset 28093

Timestamp:
May 22, 2024, 5:52:12 PM (2 months ago)
Author:
phosit
Message:

Allow to use a generator as MapGenerator

This way it's clear what's the input and what's the output of the computation.
All map generation scripts should reman working. They are adopted in a future commit.

Engine.SetProgress and Engine.ExportMap can be removed in a future commit.

Comments by: @marder, @sera, @Stan

Differential Revision: https://code.wildfiregames.com/D5220

Location:
ps/trunk
Files:
2 added
5 edited

Legend:

Unmodified
Added
Removed
  • ps/trunk/binaries/data/mods/public/maps/random/rmgen/RandomMap.js

    r28036 r28093  
    480480};
    481481
    482 RandomMap.prototype.ExportMap = function()
     482RandomMap.prototype. = function()
    483483{
    484484    if (g_Environment.Water.WaterBody.Height === undefined)
     
    487487    this.logger.close();
    488488
    489     Engine.ExportMap({
     489    {
    490490        "entities": this.exportEntityList(),
    491491        "height": this.exportHeightData(),
     
    496496        "Camera": g_Camera,
    497497        "Environment": g_Environment
    498     });
    499 };
     498    };
     499};
     500
     501RandomMap.prototype.ExportMap = function()
     502{
     503    Engine.ExportMap(this.MakeExportable());
     504};
  • ps/trunk/binaries/data/tests/test_setup.js

    r23460 r28093  
    3131    if (x !== y)
    3232        fail("Expected equal, got " + uneval(x) + " !== " + uneval(y));
     33
     34
     35
     36
     37
     38
    3339};
    3440
  • ps/trunk/source/graphics/MapGenerator.cpp

    r27973 r28093  
    1 /* Copyright (C) 2023 Wildfire Games.
     1/* Copyright (C) 202 Wildfire Games.
    22 * This file is part of 0 A.D.
    33 *
     
    4848namespace
    4949{
     50
     51
    5052bool MapGenerationInterruptCallback(JSContext* UNUSED(cx))
    5153{
     
    424426    }
    425427
    426     return mapData;
     428    LOGMESSAGE("Run RMS generator");
     429    bool hasGenerator;
     430    JS::RootedObject globalAsObject{rq.cx, &JS::HandleValue{global}.toObject()};
     431    if (!JS_HasProperty(rq.cx, globalAsObject, GENERATOR_NAME, &hasGenerator))
     432    {
     433        LOGERROR("RunMapGenerationScript: failed to search `%s`.", GENERATOR_NAME);
     434        return nullptr;
     435    }
     436
     437    if (mapData != nullptr)
     438    {
     439        LOGWARNING("The map generation script called `Engine.ExportMap` that's deprecated. The "
     440            "generator based interface should be used.");
     441        if (hasGenerator)
     442            LOGWARNING("The map generation script contains a `%s` but `Engine.ExportMap` was already "
     443                "called. `%s` isn't called, preserving the old behavior.", GENERATOR_NAME,
     444                GENERATOR_NAME);
     445        return mapData;
     446    }
     447
     448    try
     449    {
     450        JS::RootedValue map{rq.cx, ScriptFunction::RunGenerator(rq, global, GENERATOR_NAME, settingsVal,
     451            [&](const JS::HandleValue value)
     452            {
     453                int tempProgress;
     454                if (!Script::FromJSVal(rq, value, tempProgress))
     455                    throw std::runtime_error{"Failed to convert the yielded value to an "
     456                        "integer."};
     457                progress.store(tempProgress);
     458            })};
     459
     460        JS::RootedValue exportedMap{rq.cx};
     461        const bool exportSuccess{ScriptFunction::Call(rq, map, "MakeExportable", &exportedMap)};
     462        return Script::WriteStructuredClone(rq, exportSuccess ? exportedMap : map);
     463    }
     464    catch(const std::exception& e)
     465    {
     466        LOGERROR("%s", e.what());
     467        return nullptr;
     468    }
     469    catch(...)
     470    {
     471        return nullptr;
     472    }
    427473}
  • ps/trunk/source/graphics/tests/test_MapGenerator.h

    r27965 r28093  
    1 /* Copyright (C) 2023 Wildfire Games.
     1/* Copyright (C) 202 Wildfire Games.
    22 * This file is part of 0 A.D.
    33 *
     
    5252        for (const VfsPath& path : paths)
    5353        {
     54
    5455            ScriptInterface scriptInterface("Engine", "MapGenerator", g_ScriptContext);
    5556            ScriptTestSetup(scriptInterface);
    5657
    57             // It's never read in the test so it doesn't matter to what value it's initialized. For
    58             // good practice it's initialized to 1.
    5958            std::atomic<int> progress{1};
    6059
     
    6261                path, "{\"Seed\": 0}", JSPROP_ENUMERATE | JSPROP_PERMANENT)};
    6362
    64             // The test scripts don't call `ExportMap` so `RunMapGenerationScript` allways returns
    65             // `nullptr`.
    66             TS_ASSERT_EQUALS(result, nullptr);
     63            if (path == "maps/random/tests/test_Generator.js" ||
     64                path == "maps/random/tests/test_RecoverableError.js")
     65            {
     66                TS_ASSERT_EQUALS(progress.load(), 50);
     67                TS_ASSERT_DIFFERS(result, nullptr);
     68            }
     69            else
     70            {
     71                // The test scripts don't call `ExportMap` so `RunMapGenerationScript` allways
     72                // returns `nullptr`.
     73                TS_ASSERT_EQUALS(result, nullptr);
     74                // Because the test scripts don't call `ExportMap`, `GenerateMap` is searched, which
     75                // doesn't exist.
     76                TS_ASSERT_STR_CONTAINS(logger.GetOutput(),
     77                    "Failed to call the generator `GenerateMap`.");
     78            }
    6779        }
    6880    }
  • ps/trunk/source/scriptinterface/FunctionWrapper.h

    r27965 r28093  
    1 /* Copyright (C) 2023 Wildfire Games.
     1/* Copyright (C) 202 Wildfire Games.
    22 * This file is part of 0 A.D.
    33 *
     
    2323#include "ScriptRequest.h"
    2424
     25
    2526#include <tuple>
    2627#include <type_traits>
     28
    2729#include <utility>
    2830
     
    8183    struct args_info<R(C::*)(Types ...) const> : public args_info<R(C::*)(Types ...)> {};
    8284
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
    8396    ///////////////////////////////////////////////////////////////////////////
    8497    ///////////////////////////////////////////////////////////////////////////
     
    348361
    349362    /**
     363
     364
     365
     366
     367
     368
     369
     370
     371
     372
     373
     374
     375
     376
     377
     378
     379
     380
     381
     382
     383
     384
     385
     386
     387
     388
     389
     390
     391
     392
     393
     394
     395
     396
     397
     398
     399
     400
     401
     402
     403
     404
     405
     406
     407
     408
     409
     410
     411
     412
     413
     414
     415
    350416     * Return a function spec from a C++ function.
    351417     */
Note: See TracChangeset for help on using the changeset viewer.