Add NativeArchiveShelf which can be used to access archives faster, especially within...
[SquirrelJME.git] / emulators / springcoat-vm / src / main / java / cc / squirreljme / vm / springcoat / MLENativeArchive.java
bloba288d02d355ecfea2c62049d46ecf2b1d96ed231
1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // Multi-Phasic Applications: SquirrelJME
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the Mozilla Public License Version 2.0.
7 // See license.mkd for licensing and copyright information.
8 // ---------------------------------------------------------------------------
10 package cc.squirreljme.vm.springcoat;
12 import cc.squirreljme.jvm.mle.NativeArchiveShelf;
13 import cc.squirreljme.jvm.mle.brackets.NativeArchiveBracket;
14 import cc.squirreljme.jvm.mle.brackets.NativeArchiveEntryBracket;
15 import cc.squirreljme.jvm.mle.exceptions.MLECallError;
16 import cc.squirreljme.vm.springcoat.brackets.NativeArchiveEntryObject;
17 import cc.squirreljme.vm.springcoat.brackets.NativeArchiveObject;
18 import cc.squirreljme.vm.springcoat.exceptions.SpringMLECallError;
19 import java.io.IOException;
21 /**
22 * SpringCoat layer for {@link NativeArchiveShelf}.
24 * @since 2024/03/05
26 public enum MLENativeArchive
27 implements MLEFunction
29 /** {@link NativeArchiveShelf#archiveClose(NativeArchiveBracket)}. */
30 ARCHIVE_CLOSE("archiveClose:" +
31 "(Lcc/squirreljme/jvm/mle/brackets/NativeArchiveBracket;)V")
33 /**
34 * {@inheritDoc}
35 * @since 2024/03/05
37 @Override
38 public Object handle(SpringThreadWorker __thread, Object... __args)
40 Object archive = __args[0];
42 try
44 NativeArchiveShelf.archiveClose(
45 MLENativeArchive.__archiveObject(archive).wrapped);
47 catch (MLECallError __e)
49 throw new SpringMLECallError(__e);
52 return null;
56 /**
57 * {@link NativeArchiveShelf#archiveEntry(NativeArchiveBracket,
58 * String)}.
60 ARCHIVE_ENTRY("archiveEntry:" +
61 "(Lcc/squirreljme/jvm/mle/brackets/NativeArchiveBracket;" +
62 "Ljava/lang/String)" +
63 "Lcc/squirreljme/jvm/mle/brackets/NativeArchiveEntryBracket;")
65 /**
66 * {@inheritDoc}
67 * @since 2024/03/05
69 @Override
70 public Object handle(SpringThreadWorker __thread, Object... __args)
72 Object archive = __args[0];
73 String name = __thread.asNativeObject(String.class, __args[1]);
75 try
77 NativeArchiveEntryBracket entry =
78 NativeArchiveShelf.archiveEntry(
79 MLENativeArchive.__archiveObject(archive).wrapped,
80 name);
81 if (entry == null)
82 return SpringNullObject.NULL;
84 return new NativeArchiveEntryObject(__thread.machine,
85 entry);
87 catch (MLECallError __e)
89 throw new SpringMLECallError(__e);
94 /** {@link NativeArchiveShelf#archiveOpenZip(byte[], int, int)}. */
95 ARCHIVE_OPEN_ZIP("archiveOpenZip:" +
96 "([BII)Lcc/squirreljme/jvm/mle/brackets/NativeArchiveBracket;")
98 /**
99 * {@inheritDoc}
100 * @since 2024/03/05
102 @Override
103 public Object handle(SpringThreadWorker __thread, Object... __args)
105 SpringArrayObjectByte buf = (SpringArrayObjectByte)__args[0];
106 int off = (Integer)__args[1];
107 int len = (Integer)__args[2];
111 return new NativeArchiveObject(__thread.machine,
112 NativeArchiveShelf.archiveOpenZip(buf.array(),
113 off, len));
115 catch (MLECallError __e)
117 throw new SpringMLECallError(__e);
123 * {@link NativeArchiveShelf#entryIsDirectory(NativeArchiveEntryBracket)}.
125 ENTRY_IS_DIRECTORY("entryIsDirectory:" +
126 "(Lcc/squirreljme/jvm/mle/brackets/NativeArchiveEntryBracket;)Z")
129 * {@inheritDoc}
130 * @since 2024/03/05
132 @Override
133 public Object handle(SpringThreadWorker __thread, Object... __args)
135 NativeArchiveEntryObject entry =
136 MLENativeArchive.__entryObject(__args[0]);
140 return NativeArchiveShelf.entryIsDirectory(entry.wrapped);
142 catch (MLECallError __e)
144 throw new SpringMLECallError(__e);
149 /** {@link NativeArchiveShelf#entryOpen(NativeArchiveEntryBracket)}. */
150 ENTRY_OPEN("entryOpen:" +
151 "(Lcc/squirreljme/jvm/mle/brackets/NativeArchiveEntryBracket;)" +
152 "Ljava/io/InputStream;")
155 * {@inheritDoc}
156 * @since 2024/03/05
158 @Override
159 public Object handle(SpringThreadWorker __thread, Object... __args)
161 NativeArchiveEntryObject entry =
162 MLENativeArchive.__entryObject(__args[0]);
166 return __thread.proxyInputStream(
167 NativeArchiveShelf.entryOpen(entry.wrapped));
169 catch (MLECallError|IOException __e)
171 throw new SpringMLECallError(__e);
177 * {@link NativeArchiveShelf#entryUncompressedSize(
178 * NativeArchiveEntryBracket)}.
180 ENTRY_UNCOMPRESSED_SIZE("entryUncompressedSize:" +
181 "(Lcc/squirreljme/jvm/mle/brackets/NativeArchiveEntryBracket;)J")
184 * {@inheritDoc}
185 * @since 2024/03/05
187 @Override
188 public Object handle(SpringThreadWorker __thread, Object... __args)
190 NativeArchiveEntryObject entry =
191 MLENativeArchive.__entryObject(__args[0]);
195 return NativeArchiveShelf.entryUncompressedSize(entry.wrapped);
197 catch (MLECallError __e)
199 throw new SpringMLECallError(__e);
204 /** End. */
207 /** The dispatch key. */
208 protected final String key;
211 * Initializes the dispatcher info.
213 * @param __key The key.
214 * @throws NullPointerException On null arguments.
215 * @since 2024/03/05
217 MLENativeArchive(String __key)
218 throws NullPointerException
220 if (__key == null)
221 throw new NullPointerException("NARG");
223 this.key = __key;
227 * {@inheritDoc}
228 * @since 2024/03/05
230 @Override
231 public String key()
233 return this.key;
237 * Checks if this is a {@link NativeArchiveObject}.
239 * @param __object The object to check.
240 * @return As one if this is one.
241 * @throws SpringMLECallError If this is not one.
242 * @since 2024/03/05
244 static NativeArchiveObject __archiveObject(Object __object)
245 throws SpringMLECallError
247 if (!(__object instanceof NativeArchiveObject))
248 throw new SpringMLECallError("Not a NativeArchiveObject.");
250 return (NativeArchiveObject)__object;
254 * Checks if this is a {@link NativeArchiveEntryObject}.
256 * @param __object The object to check.
257 * @return As one if this is one.
258 * @throws SpringMLECallError If this is not one.
259 * @since 2024/03/05
261 static NativeArchiveEntryObject __entryObject(Object __object)
262 throws SpringMLECallError
264 if (!(__object instanceof NativeArchiveEntryObject))
265 throw new SpringMLECallError(
266 "Not a NativeArchiveEntryObject.");
268 return (NativeArchiveEntryObject)__object;