2828import net .minecraft .client .gui .screens .Screen ;
2929import net .minecraft .nbt .CompoundTag ;
3030import net .minecraft .nbt .NbtIo ;
31+ import org .apache .commons .io .FilenameUtils ;
3132import org .lwjgl .BufferUtils ;
3233import org .lwjgl .PointerBuffer ;
3334import org .lwjgl .system .MemoryUtil ;
4142import java .nio .file .Path ;
4243import java .util .ArrayList ;
4344import java .util .List ;
45+ import java .util .Optional ;
4446
4547import static meteordevelopment .meteorclient .MeteorClient .mc ;
4648
@@ -98,12 +100,12 @@ public void initWidgets() {
98100 if (imported != null )
99101 MeteorClient .LOG .info ("Successfully imported profile '{}'." , imported .name .get ());
100102 reload ();
101- } catch (IOException e ) {
103+ } catch (Exception e ) {
102104 MeteorClient .LOG .error ("Error importing profile" , e );
103105 OkPrompt .create ()
104106 .title ("Failure importing profile" )
105107 .message ("There was an error importing the profile." )
106- .message ("Error: %d " , e .getMessage ())
108+ .message ("Error: %s " , e .getMessage ())
107109 .dontShowAgainCheckboxVisible (false )
108110 .show ();
109111 }
@@ -146,15 +148,25 @@ private Profile importProfile() throws IOException {
146148 File profileFile = new File (file );
147149
148150 CompoundTag nbt = NbtIo .read (profileFile .toPath ());
151+ if (nbt == null ) return null ;
149152
150153 Profile p = new Profile ();
151- if (!p .name .set (nbt .getStringOr ("name" , profileFile .getName ()))) return null ;
152- File profileFolder = p .getSafeFile ();
153- if (profileFolder == null ) return null ;
154+ Optional <String > parsedName = nbt .getString ("name" ).filter (n -> !n .isEmpty ());
155+
156+ if (parsedName .filter (p .name ::set ).isEmpty () && !p .name .set (FilenameUtils .removeExtension (profileFile .getName ()))) {
157+ throw new IllegalStateException ("Imported profile does not have a valid name." );
158+ }
159+
160+ File profileFolder = p .getFile ().getCanonicalFile ();
161+ if (!profileFolder .getParentFile ().equals (Profiles .FOLDER .getCanonicalFile ())) {
162+ throw new IllegalStateException ("Imported profile does not have a valid location." );
163+ }
164+
154165 //noinspection ResultOfMethodCallIgnored
155166 profileFolder .mkdirs ();
156-
157167 nbt .remove ("name" );
168+
169+ boolean valid = false ;
158170 for (var entry : nbt .entrySet ()) {
159171 String filename = entry .getKey ();
160172 if (!filename .endsWith (".nbt" )) continue ;
@@ -172,9 +184,14 @@ private Profile importProfile() throws IOException {
172184 File f = new File (profileFolder , filename ).getCanonicalFile ();
173185 if (!f .toPath ().startsWith (profileFolder .toPath ())) continue ;
174186
187+ valid = true ;
175188 NbtIo .writeUnnamedTagWithFallback (entry .getValue (), new DataOutputStream (new FileOutputStream (f )));
176189 }
177190
191+ if (!valid ) {
192+ throw new IllegalStateException ("Imported file is not a profile." );
193+ }
194+
178195 Profiles .get ().getAll ().add (p );
179196 Profiles .get ().save ();
180197
@@ -215,7 +232,7 @@ public void initWidgets() {
215232
216233 WButton save = add (theme .button (isNew ? "Create" : "Save" )).expandX ().widget ();
217234 save .action = () -> {
218- if (profile .getSafeFile () == null ) return ;
235+ if (profile .name . get (). isEmpty () ) return ;
219236
220237 if (isNew ) {
221238 for (Profile p : Profiles .get ()) {
0 commit comments