1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-08-28 08:12:18 +00:00

[Patchwork] Fix Network Config

Network config should now work in a stable mannor, the previous person did not port over the imports and thats why this broke in the first place, possibly required import did not exist for fabric at the time?
This commit is contained in:
Merith-TK 2021-03-27 14:44:47 -07:00
parent 46bc42d5a7
commit 2c3573719b
2 changed files with 124 additions and 113 deletions

View File

@ -29,6 +29,8 @@ dependencies {
modImplementation "net.fabricmc:fabric-loader:${fabric_loader_version}"
modImplementation "net.fabricmc.fabric-api:fabric-api:${fabric_api_version}"
compile 'com.electronwill.night-config:json:3.6.0'
modImplementation "me.shedaniel.cloth:config-2:${cloth_config_version}"
modImplementation "io.github.prospector:modmenu:${modmenu_version}"

View File

@ -6,120 +6,129 @@
package dan200.computercraft.core.apis.http.options;
import com.electronwill.nightconfig.core.CommentedConfig;
import com.electronwill.nightconfig.core.Config;
import com.electronwill.nightconfig.core.InMemoryCommentedFormat;
import com.electronwill.nightconfig.core.UnmodifiableConfig;
import dan200.computercraft.ComputerCraft;
import javax.annotation.Nullable;
import java.util.Locale;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
public class AddressRuleConfig {
// TODO haha config is gone, do fix
// TODO FIGURE OUT WHY THE HELL THE PREVIOUS GUY HAD TO COMMENT THIS OUT
public static UnmodifiableConfig makeRule( String host, Action action )
{
CommentedConfig config = InMemoryCommentedFormat.defaultInstance().createConfig( ConcurrentHashMap::new );
config.add( "host", host );
config.add( "action", action.name().toLowerCase( Locale.ROOT ) );
// public static UnmodifiableConfig makeRule( String host, Action action )
// {
// CommentedConfig config = InMemoryCommentedFormat.defaultInstance().createConfig( ConcurrentHashMap::new );
// config.add( "host", host );
// config.add( "action", action.name().toLowerCase( Locale.ROOT ) );
//
// if( host.equals( "*" ) && action == Action.ALLOW )
// {
// config.setComment( "timeout", "The period of time (in milliseconds) to wait before a HTTP request times out. Set to 0 for unlimited." );
// config.add( "timeout", AddressRule.TIMEOUT );
//
// config.setComment( "max_download", "The maximum size (in bytes) that a computer can download in a single request. Note that responses
// may receive more data than allowed, but this data will not be returned to the client." );
// config.set( "max_download", AddressRule.MAX_DOWNLOAD );
//
// config.setComment( "max_upload", "The maximum size (in bytes) that a computer can upload in a single request. This includes headers and
// POST text." );
// config.set( "max_upload", AddressRule.MAX_UPLOAD );
//
// config.setComment( "max_websocket_message", "The maximum size (in bytes) that a computer can send or receive in one websocket packet." );
// config.set( "max_websocket_message", AddressRule.WEBSOCKET_MESSAGE );
// }
//
// return config;
// }
//
// public static boolean checkRule( UnmodifiableConfig builder )
// {
// String hostObj = get( builder, "host", String.class ).orElse( null );
// return hostObj != null && checkEnum( builder, "action", Action.class )
// && check( builder, "port", Number.class )
// && check( builder, "timeout", Number.class )
// && check( builder, "max_upload", Number.class )
// && check( builder, "max_download", Number.class )
// && check( builder, "websocket_message", Number.class )
// && AddressRule.parse( hostObj, port, PartialOptions.DEFAULT ) != null;
// }
//
// @Nullable
// public static AddressRule parseRule( UnmodifiableConfig builder )
// {
// String hostObj = get( builder, "host", String.class ).orElse( null );
// Integer port = get( builder, "port", Number.class ).map( Number::intValue ).orElse( null );
// if( hostObj == null ) return null;
//
// Action action = getEnum( builder, "action", Action.class ).orElse( null );
// Integer timeout = get( builder, "timeout", Number.class ).map( Number::intValue ).orElse( null );
// Long maxUpload = get( builder, "max_upload", Number.class ).map( Number::longValue ).orElse( null );
// Long maxDownload = get( builder, "max_download", Number.class ).map( Number::longValue ).orElse( null );
// Integer websocketMessage = get( builder, "websocket_message", Number.class ).map( Number::intValue ).orElse( null );
//
// PartialOptions options = new PartialOptions(
// action,
// maxUpload,
// maxDownload,
// timeout,
// websocketMessage
// );
//
// return AddressRule.parse( hostObj, port, options );
// }
//
// private static <T> boolean check( UnmodifiableConfig config, String field, Class<T> klass )
// {
// Object value = config.get( field );
// if( value == null || klass.isInstance( value ) ) return true;
//
// ComputerCraft.log.warn( "HTTP rule's {} is not a {}.", field, klass.getSimpleName() );
// return false;
// }
//
// private static <T extends Enum<T>> boolean checkEnum( UnmodifiableConfig config, String field, Class<T> klass )
// {
// Object value = config.get( field );
// if( value == null ) return true;
//
// if( !(value instanceof String) )
// {
// ComputerCraft.log.warn( "HTTP rule's {} is not a string", field );
// return false;
// }
//
// if( parseEnum( klass, (String) value ) == null )
// {
// ComputerCraft.log.warn( "HTTP rule's {} is not a known option", field );
// return false;
// }
//
// return true;
// }
//
// private static <T> Optional<T> get( UnmodifiableConfig config, String field, Class<T> klass )
// {
// Object value = config.get( field );
// return klass.isInstance( value ) ? Optional.of( klass.cast( value ) ) : Optional.empty();
// }
//
// private static <T extends Enum<T>> Optional<T> getEnum( UnmodifiableConfig config, String field, Class<T> klass )
// {
// return get( config, field, String.class ).map( x -> parseEnum( klass, x ) );
// }
//
// @Nullable
// private static <T extends Enum<T>> T parseEnum( Class<T> klass, String x )
// {
// for( T value : klass.getEnumConstants() )
// {
// if( value.name().equalsIgnoreCase( x ) ) return value;
// }
// return null;
// }
if( host.equals( "*" ) && action == Action.ALLOW )
{
config.setComment( "timeout", "The period of time (in milliseconds) to wait before a HTTP request times out. Set to 0 for unlimited." );
config.add( "timeout", AddressRule.TIMEOUT );
config.setComment( "max_download", "The maximum size (in bytes) that a computer can download in a single request. Note that responses may receive more data than allowed, but this data will not be returned to the client." );
config.set( "max_download", AddressRule.MAX_DOWNLOAD );
config.setComment( "max_upload", "The maximum size (in bytes) that a computer can upload in a single request. This includes headers and POST text." );
config.set( "max_upload", AddressRule.MAX_UPLOAD );
config.setComment( "max_websocket_message", "The maximum size (in bytes) that a computer can send or receive in one websocket packet." );
config.set( "max_websocket_message", AddressRule.WEBSOCKET_MESSAGE );
}
return config;
}
public static boolean checkRule( UnmodifiableConfig builder )
{
String hostObj = get( builder, "host", String.class ).orElse( null );
Integer port = get( builder, "port", Number.class ).map( Number::intValue ).orElse( null );
return hostObj != null && checkEnum( builder, "action", Action.class )
&& check( builder, "port", Number.class )
&& check( builder, "timeout", Number.class )
&& check( builder, "max_upload", Number.class )
&& check( builder, "max_download", Number.class )
&& check( builder, "websocket_message", Number.class )
&& AddressRule.parse( hostObj, port, PartialOptions.DEFAULT ) != null;
}
@Nullable
public static AddressRule parseRule( UnmodifiableConfig builder )
{
String hostObj = get( builder, "host", String.class ).orElse( null );
Integer port = get( builder, "port", Number.class ).map( Number::intValue ).orElse( null );
if( hostObj == null ) return null;
Action action = getEnum( builder, "action", Action.class ).orElse( null );
Integer timeout = get( builder, "timeout", Number.class ).map( Number::intValue ).orElse( null );
Long maxUpload = get( builder, "max_upload", Number.class ).map( Number::longValue ).orElse( null );
Long maxDownload = get( builder, "max_download", Number.class ).map( Number::longValue ).orElse( null );
Integer websocketMessage = get( builder, "websocket_message", Number.class ).map( Number::intValue ).orElse( null );
PartialOptions options = new PartialOptions(
action,
maxUpload,
maxDownload,
timeout,
websocketMessage
);
return AddressRule.parse( hostObj, port, options );
}
private static <T> boolean check( UnmodifiableConfig config, String field, Class<T> klass )
{
Object value = config.get( field );
if( value == null || klass.isInstance( value ) ) return true;
ComputerCraft.log.warn( "HTTP rule's {} is not a {}.", field, klass.getSimpleName() );
return false;
}
private static <T extends Enum<T>> boolean checkEnum( UnmodifiableConfig config, String field, Class<T> klass )
{
Object value = config.get( field );
if( value == null ) return true;
if( !(value instanceof String) )
{
ComputerCraft.log.warn( "HTTP rule's {} is not a string", field );
return false;
}
if( parseEnum( klass, (String) value ) == null )
{
ComputerCraft.log.warn( "HTTP rule's {} is not a known option", field );
return false;
}
return true;
}
private static <T> Optional<T> get( UnmodifiableConfig config, String field, Class<T> klass )
{
Object value = config.get( field );
return klass.isInstance( value ) ? Optional.of( klass.cast( value ) ) : Optional.empty();
}
private static <T extends Enum<T>> Optional<T> getEnum( UnmodifiableConfig config, String field, Class<T> klass )
{
return get( config, field, String.class ).map( x -> parseEnum( klass, x ) );
}
@Nullable
private static <T extends Enum<T>> T parseEnum( Class<T> klass, String x )
{
for( T value : klass.getEnumConstants() )
{
if( value.name().equalsIgnoreCase( x ) ) return value;
}
return null;
}
}