Skip to content

Commit e7cf585

Browse files
feat: Rewrite config (#21)
* πŸΊπŸš§πŸ“•πŸ”§πŸ’»βœ”οΈπŸ’ΎπŸš€οΈ * πŸΊπŸš§πŸ“•πŸ”§πŸ’»βœ”οΈπŸ’ΎπŸš€οΈ * πŸΊπŸ“° * chore: Apply Spotless * πŸΊβ“ * πŸΊπŸš§πŸš€ * chore: Apply Spotless * πŸΊπŸ¦€ * πŸΊπŸ’ΎπŸͺ‘πŸ”© * chore: Apply Spotless * πŸΊπŸ’ΎπŸͺ‘πŸ”© * chore: Apply Spotless * 🐺πŸͺ‘πŸ”©πŸš€ * chore: Apply Spotless * πŸΊπŸš€πŸŽΆ * chore: Apply Spotless * 🐺❌ * chore: Apply Spotless * πŸΊπŸ“°πŸŽΆ * chore: Apply Spotless --------- Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 0008611 commit e7cf585

File tree

31 files changed

+801
-1069
lines changed

31 files changed

+801
-1069
lines changed

β€Ž.gitignoreβ€Ž

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,22 @@ build
1010

1111
run
1212
.DS_Store
13-
.architectury-transformer
13+
.architectury-transformer
14+
15+
# Eclipse
16+
.loadpath
17+
.metadata
18+
.settings
19+
20+
**/.classpath
21+
**/.project
22+
23+
# NetBeans
24+
nbbuild
25+
dist
26+
nbdist
27+
.nb-gradle
28+
29+
**/nbproject/private
30+
**/nbproject/Makefile-*.mk
31+
**/nbproject/Package-*.bash

β€Žbase/common/src/main/java/band/kessoku/lib/api/base/reflect/ReflectUtil.javaβ€Ž

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@
1515
*/
1616
package band.kessoku.lib.api.base.reflect;
1717

18+
import java.lang.reflect.AccessibleObject;
1819
import java.lang.reflect.Field;
1920
import java.util.Arrays;
2021

22+
import band.kessoku.lib.api.KessokuLib;
23+
2124
public final class ReflectUtil {
2225
private ReflectUtil() {
2326
}
@@ -31,4 +34,14 @@ public static boolean isAssignableFrom(Object o, Class<?>... classes) {
3134
var flag = Arrays.stream(classes).anyMatch(clazz -> !o.getClass().isAssignableFrom(clazz));
3235
return !flag;
3336
}
37+
38+
public static boolean markAccessible(AccessibleObject obj) {
39+
try {
40+
obj.setAccessible(true);
41+
return true;
42+
} catch (Exception e) {
43+
KessokuLib.getLogger().error(e.getMessage(), e);
44+
return false;
45+
}
46+
}
3447
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright (c) 2024 KessokuTeaTime
3+
*
4+
* Licensed under the GNU Lesser General Pubic License, Version 3 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.gnu.org/licenses/lgpl-3.0.html
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package band.kessoku.lib.api.config;
17+
18+
import java.lang.annotation.*;
19+
20+
/**
21+
* Config certainly allows comment, and multi-line comment.
22+
* It is usually used to explain the purpose of an entry or to fill in the format, etc.
23+
*
24+
* <p>This annotation should be placed on the config's field.
25+
*
26+
* {@snippet :
27+
* import band.kessoku.lib.config.api.config.Config;
28+
* import band.kessoku.lib.config.api.config.Name;
29+
* import band.kessoku.lib.config.api.config.Comments;
30+
* import band.kessoku.lib.config.values.config.StringValue;
31+
*
32+
* @Config(modid="mymodid", serialize="json5")
33+
* public class MyConfig {
34+
* @Comment("First comment")
35+
* @Comment("Second comment")
36+
* @Name("someoneField")
37+
* public static final StringValue SOMEONE_FIELD = new StringValue("test");
38+
* }
39+
*}
40+
*
41+
* <p>and in config:
42+
*
43+
* {@snippet :
44+
* // First comment
45+
* // Second comment
46+
* someoneField = test
47+
* }
48+
*
49+
* @see Config @Config
50+
* @see Name @Name
51+
*
52+
* @author AmarokIce
53+
*/
54+
@Repeatable(Comments.class)
55+
@Target(ElementType.FIELD)
56+
@Retention(RetentionPolicy.RUNTIME)
57+
public @interface Comment {
58+
String value();
59+
}
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,20 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package band.kessoku.lib.api.config.annotations;
16+
package band.kessoku.lib.api.config;
1717

1818
import java.lang.annotation.ElementType;
1919
import java.lang.annotation.Retention;
2020
import java.lang.annotation.RetentionPolicy;
2121
import java.lang.annotation.Target;
2222

23-
@Retention(RetentionPolicy.RUNTIME)
23+
/**
24+
* @see Comment
25+
*
26+
* @author AmarokIce
27+
*/
2428
@Target(ElementType.FIELD)
29+
@Retention(RetentionPolicy.RUNTIME)
2530
public @interface Comments {
2631
Comment[] value();
2732
}

β€Žconfig/common/src/main/java/band/kessoku/lib/api/config/Config.javaβ€Ž

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,53 @@
1515
*/
1616
package band.kessoku.lib.api.config;
1717

18-
import band.kessoku.lib.impl.config.AbstractConfig;
1918

20-
public class Config extends AbstractConfig {
19+
import java.lang.annotation.ElementType;
20+
import java.lang.annotation.Retention;
21+
import java.lang.annotation.RetentionPolicy;
22+
import java.lang.annotation.Target;
23+
24+
import band.kessoku.lib.api.config.serializer.ConfigSerializers;
25+
26+
/**
27+
* All configs started here to create a new config.
28+
*
29+
* <p>The only necessary data is modid, if config name is empty, it will fill by modid.
30+
* The registration process is automatic, you need to put the secondary bet liberation
31+
* at the head of the config class.
32+
*
33+
* <p>We provide some default {@link ConfigSerializer} in {@link ConfigSerializers}.
34+
*
35+
* <p>And the new config {@code ConfigSerializer} should registry in {@link ConfigSerializers#register}.
36+
*
37+
* {@snippet :
38+
* @Config(modid="mymodid", serialize="json5")
39+
* public class MyConfig {
40+
* @Comment("First comment")
41+
* @Comment("Second comment")
42+
* @Name("someoneField")
43+
* public static String SOMEONE_FIELD = "test";
44+
* }
45+
*}
46+
*
47+
* <p>and in config:
48+
*
49+
* {@snippet :
50+
* // First comment
51+
* // Second comment
52+
* someoneField = test
53+
* }
54+
*
55+
* @see Comment
56+
* @see Name
57+
* @see ConfigSerializers
58+
*
59+
* @author AmarokIce
60+
*/
61+
@Target(ElementType.TYPE)
62+
@Retention(RetentionPolicy.RUNTIME)
63+
public @interface Config {
64+
String modid();
65+
String name() default "";
66+
String serialize() default "toml";
2167
}

β€Žconfig/common/src/main/java/band/kessoku/lib/api/config/ConfigSerializer.javaβ€Ž

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,26 @@
1515
*/
1616
package band.kessoku.lib.api.config;
1717

18-
import java.util.Map;
19-
20-
import band.kessoku.lib.impl.config.AbstractConfig;
18+
import band.kessoku.lib.api.config.exception.IllegalValueException;
19+
import band.kessoku.lib.api.config.serializer.Json5ConfigSerializer;
20+
import band.kessoku.lib.api.config.serializer.JsonConfigSerializer;
21+
import band.kessoku.lib.api.config.serializer.TomlConfigSerializer;
2122

23+
/**
24+
* @see JsonConfigSerializer
25+
* @see Json5ConfigSerializer
26+
* @see TomlConfigSerializer
27+
*
28+
* @author AmarokIce
29+
*/
2230
public interface ConfigSerializer {
23-
String serialize(Map<String, AbstractConfig.WrappedValue> valueMap);
24-
25-
Map<String, Object> deserialize(String value);
31+
/**
32+
* @throws IllegalValueException If config name cannot be encoded, throw IllegalValueException.
33+
*/
34+
void serializer(String valueStr, Class<?> clazz) throws IllegalValueException;
2635

27-
String getFileExtension();
36+
/**
37+
* @return raw data will fill in config.
38+
*/
39+
String deserializer(Class<?> clazz);
2840
}

β€Žconfig/common/src/main/java/band/kessoku/lib/api/config/ConfigValue.javaβ€Ž

Lines changed: 0 additions & 126 deletions
This file was deleted.

0 commit comments

Comments
Β (0)