mirror of
				https://github.com/SquidDev-CC/CC-Tweaked
				synced 2025-10-30 13:13:00 +00:00 
			
		
		
		
	Make the list of API factories per-ComputerContext
The registry is still a singleton inside the Minecraft code, but this makes the core a little cleaner.
This commit is contained in:
		| @@ -19,7 +19,6 @@ import dan200.computercraft.api.pocket.PocketUpgradeSerialiser; | |||||||
| import dan200.computercraft.api.redstone.BundledRedstoneProvider; | import dan200.computercraft.api.redstone.BundledRedstoneProvider; | ||||||
| import dan200.computercraft.api.turtle.TurtleRefuelHandler; | import dan200.computercraft.api.turtle.TurtleRefuelHandler; | ||||||
| import dan200.computercraft.api.turtle.TurtleUpgradeSerialiser; | import dan200.computercraft.api.turtle.TurtleUpgradeSerialiser; | ||||||
| import dan200.computercraft.core.apis.ApiFactories; |  | ||||||
| import dan200.computercraft.core.asm.GenericMethod; | import dan200.computercraft.core.asm.GenericMethod; | ||||||
| import dan200.computercraft.core.filesystem.WritableFileMount; | import dan200.computercraft.core.filesystem.WritableFileMount; | ||||||
| import dan200.computercraft.impl.detail.DetailRegistryImpl; | import dan200.computercraft.impl.detail.DetailRegistryImpl; | ||||||
|   | |||||||
| @@ -2,7 +2,7 @@ | |||||||
| // | // | ||||||
| // SPDX-License-Identifier: MPL-2.0 | // SPDX-License-Identifier: MPL-2.0 | ||||||
| 
 | 
 | ||||||
| package dan200.computercraft.core.apis; | package dan200.computercraft.impl; | ||||||
| 
 | 
 | ||||||
| import dan200.computercraft.api.lua.ILuaAPIFactory; | import dan200.computercraft.api.lua.ILuaAPIFactory; | ||||||
| 
 | 
 | ||||||
| @@ -23,7 +23,7 @@ public final class ApiFactories { | |||||||
|         factories.add(factory); |         factories.add(factory); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public static Iterable<ILuaAPIFactory> getAll() { |     public static Collection<ILuaAPIFactory> getAll() { | ||||||
|         return factoriesView; |         return factoriesView; | ||||||
|     } |     } | ||||||
| } | } | ||||||
| @@ -15,6 +15,7 @@ import dan200.computercraft.core.computer.mainthread.MainThreadConfig; | |||||||
| import dan200.computercraft.core.lua.CobaltLuaMachine; | import dan200.computercraft.core.lua.CobaltLuaMachine; | ||||||
| import dan200.computercraft.core.lua.ILuaMachine; | import dan200.computercraft.core.lua.ILuaMachine; | ||||||
| import dan200.computercraft.impl.AbstractComputerCraftAPI; | import dan200.computercraft.impl.AbstractComputerCraftAPI; | ||||||
|  | import dan200.computercraft.impl.ApiFactories; | ||||||
| import dan200.computercraft.shared.CommonHooks; | import dan200.computercraft.shared.CommonHooks; | ||||||
| import dan200.computercraft.shared.computer.metrics.GlobalMetrics; | import dan200.computercraft.shared.computer.metrics.GlobalMetrics; | ||||||
| import dan200.computercraft.shared.config.ConfigSpec; | import dan200.computercraft.shared.config.ConfigSpec; | ||||||
| @@ -70,6 +71,7 @@ public final class ServerContext { | |||||||
|             .computerThreads(ConfigSpec.computerThreads.get()) |             .computerThreads(ConfigSpec.computerThreads.get()) | ||||||
|             .mainThreadScheduler(mainThread) |             .mainThreadScheduler(mainThread) | ||||||
|             .luaFactory(luaMachine) |             .luaFactory(luaMachine) | ||||||
|  |             .apiFactories(ApiFactories.getAll()) | ||||||
|             .build(); |             .build(); | ||||||
|         idAssigner = new IDAssigner(storageDir.resolve("ids.json")); |         idAssigner = new IDAssigner(storageDir.resolve("ids.json")); | ||||||
|     } |     } | ||||||
|   | |||||||
| @@ -4,6 +4,7 @@ | |||||||
| 
 | 
 | ||||||
| package dan200.computercraft.core; | package dan200.computercraft.core; | ||||||
| 
 | 
 | ||||||
|  | import dan200.computercraft.api.lua.ILuaAPIFactory; | ||||||
| import dan200.computercraft.core.computer.ComputerThread; | import dan200.computercraft.core.computer.ComputerThread; | ||||||
| import dan200.computercraft.core.computer.GlobalEnvironment; | import dan200.computercraft.core.computer.GlobalEnvironment; | ||||||
| import dan200.computercraft.core.computer.mainthread.MainThreadScheduler; | import dan200.computercraft.core.computer.mainthread.MainThreadScheduler; | ||||||
| @@ -13,6 +14,8 @@ import dan200.computercraft.core.lua.ILuaMachine; | |||||||
| 
 | 
 | ||||||
| import javax.annotation.CheckReturnValue; | import javax.annotation.CheckReturnValue; | ||||||
| import javax.annotation.Nullable; | import javax.annotation.Nullable; | ||||||
|  | import java.util.Collection; | ||||||
|  | import java.util.List; | ||||||
| import java.util.Objects; | import java.util.Objects; | ||||||
| import java.util.concurrent.TimeUnit; | import java.util.concurrent.TimeUnit; | ||||||
| 
 | 
 | ||||||
| @@ -24,15 +27,18 @@ public final class ComputerContext { | |||||||
|     private final ComputerThread computerScheduler; |     private final ComputerThread computerScheduler; | ||||||
|     private final MainThreadScheduler mainThreadScheduler; |     private final MainThreadScheduler mainThreadScheduler; | ||||||
|     private final ILuaMachine.Factory luaFactory; |     private final ILuaMachine.Factory luaFactory; | ||||||
|  |     private final List<ILuaAPIFactory> apiFactories; | ||||||
| 
 | 
 | ||||||
|     ComputerContext( |     ComputerContext( | ||||||
|         GlobalEnvironment globalEnvironment, ComputerThread computerScheduler, |         GlobalEnvironment globalEnvironment, ComputerThread computerScheduler, | ||||||
|         MainThreadScheduler mainThreadScheduler, ILuaMachine.Factory luaFactory |         MainThreadScheduler mainThreadScheduler, ILuaMachine.Factory luaFactory, | ||||||
|  |         List<ILuaAPIFactory> apiFactories | ||||||
|     ) { |     ) { | ||||||
|         this.globalEnvironment = globalEnvironment; |         this.globalEnvironment = globalEnvironment; | ||||||
|         this.computerScheduler = computerScheduler; |         this.computerScheduler = computerScheduler; | ||||||
|         this.mainThreadScheduler = mainThreadScheduler; |         this.mainThreadScheduler = mainThreadScheduler; | ||||||
|         this.luaFactory = luaFactory; |         this.luaFactory = luaFactory; | ||||||
|  |         this.apiFactories = apiFactories; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
| @@ -72,6 +78,15 @@ public final class ComputerContext { | |||||||
|         return luaFactory; |         return luaFactory; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     /** | ||||||
|  |      * Additional APIs to inject into each computer. | ||||||
|  |      * | ||||||
|  |      * @return All available API factories. | ||||||
|  |      */ | ||||||
|  |     public List<ILuaAPIFactory> apiFactories() { | ||||||
|  |         return apiFactories; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     /** |     /** | ||||||
|      * Close the current {@link ComputerContext}, disposing of any resources inside. |      * Close the current {@link ComputerContext}, disposing of any resources inside. | ||||||
|      * |      * | ||||||
| @@ -119,6 +134,7 @@ public final class ComputerContext { | |||||||
|         private int threads = 1; |         private int threads = 1; | ||||||
|         private @Nullable MainThreadScheduler mainThreadScheduler; |         private @Nullable MainThreadScheduler mainThreadScheduler; | ||||||
|         private @Nullable ILuaMachine.Factory luaFactory; |         private @Nullable ILuaMachine.Factory luaFactory; | ||||||
|  |         private @Nullable List<ILuaAPIFactory> apiFactories; | ||||||
| 
 | 
 | ||||||
|         Builder(GlobalEnvironment environment) { |         Builder(GlobalEnvironment environment) { | ||||||
|             this.environment = environment; |             this.environment = environment; | ||||||
| @@ -165,6 +181,20 @@ public final class ComputerContext { | |||||||
|             return this; |             return this; | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|  |         /** | ||||||
|  |          * Set the additional {@linkplain ILuaAPIFactory APIs} to add to each computer. | ||||||
|  |          * | ||||||
|  |          * @param apis A list of API factories. | ||||||
|  |          * @return {@code this}, for chaining | ||||||
|  |          * @see ComputerContext#apiFactories() | ||||||
|  |          */ | ||||||
|  |         public Builder apiFactories(Collection<ILuaAPIFactory> apis) { | ||||||
|  |             Objects.requireNonNull(apis); | ||||||
|  |             if (apiFactories != null) throw new IllegalStateException("Main-thread scheduler already specified"); | ||||||
|  |             apiFactories = List.copyOf(apis); | ||||||
|  |             return this; | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|         /** |         /** | ||||||
|          * Create a new {@link ComputerContext}. |          * Create a new {@link ComputerContext}. | ||||||
|          * |          * | ||||||
| @@ -175,7 +205,8 @@ public final class ComputerContext { | |||||||
|                 environment, |                 environment, | ||||||
|                 new ComputerThread(threads), |                 new ComputerThread(threads), | ||||||
|                 mainThreadScheduler == null ? new NoWorkMainThreadScheduler() : mainThreadScheduler, |                 mainThreadScheduler == null ? new NoWorkMainThreadScheduler() : mainThreadScheduler, | ||||||
|                 luaFactory == null ? CobaltLuaMachine::new : luaFactory |                 luaFactory == null ? CobaltLuaMachine::new : luaFactory, | ||||||
|  |                 apiFactories == null ? List.of() : apiFactories | ||||||
|             ); |             ); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|   | |||||||
| @@ -181,7 +181,7 @@ final class ComputerExecutor { | |||||||
|         if (CoreConfig.httpEnabled) apis.add(new HTTPAPI(environment)); |         if (CoreConfig.httpEnabled) apis.add(new HTTPAPI(environment)); | ||||||
| 
 | 
 | ||||||
|         // Load in the externally registered APIs. |         // Load in the externally registered APIs. | ||||||
|         for (var factory : ApiFactories.getAll()) { |         for (var factory : context.apiFactories()) { | ||||||
|             var system = new ComputerSystem(environment); |             var system = new ComputerSystem(environment); | ||||||
|             var api = factory.create(system); |             var api = factory.create(system); | ||||||
|             if (api != null) apis.add(new ApiWrapper(api, system)); |             if (api != null) apis.add(new ApiWrapper(api, system)); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Jonathan Coates
					Jonathan Coates