Merge pull request #7997 from Adam-/enum

api: add RS Enum API
This commit is contained in:
Adam
2019-02-25 16:51:04 -05:00
committed by GitHub
6 changed files with 190 additions and 0 deletions

View File

@@ -1579,4 +1579,6 @@ public interface Client extends GameEngine
* Returns client item composition cache
*/
NodeCache getItemCompositionCache();
EnumComposition getEnum(int id);
}

View File

@@ -0,0 +1,36 @@
/*
* Copyright (c) 2019, Adam <Adam@sigterm.info>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.runelite.api;
public interface EnumComposition
{
int[] getIntVals();
String[] getStringVals();
int getIntValue(int key);
String getStringValue(int key);
}

View File

@@ -24,6 +24,8 @@
*/
package net.runelite.mixins;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
@@ -32,6 +34,7 @@ import javax.annotation.Nullable;
import javax.inject.Named;
import net.runelite.api.ChatMessageType;
import net.runelite.api.ClanMember;
import net.runelite.api.EnumComposition;
import net.runelite.api.Friend;
import net.runelite.api.GameState;
import net.runelite.api.GrandExchangeOffer;
@@ -106,6 +109,7 @@ import net.runelite.rs.api.RSChatLineBuffer;
import net.runelite.rs.api.RSClanMemberManager;
import net.runelite.rs.api.RSClient;
import net.runelite.rs.api.RSDeque;
import net.runelite.rs.api.RSEnum;
import net.runelite.rs.api.RSFriendContainer;
import net.runelite.rs.api.RSFriendManager;
import net.runelite.rs.api.RSHashTable;
@@ -176,6 +180,11 @@ public abstract class RSClientMixin implements RSClient
@Inject
static int skyboxColor;
@Inject
private final Cache<Integer, RSEnum> enumCache = CacheBuilder.newBuilder()
.maximumSize(64)
.build();
@Inject
@Override
public Callbacks getCallbacks()
@@ -1442,4 +1451,21 @@ public abstract class RSClientMixin implements RSClient
return false;
}
@Inject
@Override
public EnumComposition getEnum(int id)
{
assert isClientThread() : "getEnum must be called on client thread";
RSEnum rsEnum = enumCache.getIfPresent(id);
if (rsEnum != null)
{
return rsEnum;
}
rsEnum = getRsEnum(id);
enumCache.put(id, rsEnum);
return rsEnum;
}
}

View File

@@ -0,0 +1,75 @@
/*
* Copyright (c) 2019, Adam <Adam@sigterm.info>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.runelite.mixins;
import net.runelite.api.mixins.Inject;
import net.runelite.api.mixins.Mixin;
import net.runelite.rs.api.RSEnum;
@Mixin(RSEnum.class)
public abstract class RSEnumMixin implements RSEnum
{
@Inject
@Override
public int getIntValue(int key)
{
final int[] keys = getKeys();
if (keys == null)
{
return getDefaultInt();
}
for (int i = 0; i < keys.length; ++i)
{
if (keys[i] == key)
{
final int[] values = getIntVals();
return values[i];
}
}
return getDefaultInt();
}
@Inject
@Override
public String getStringValue(int key)
{
final int[] keys = getKeys();
if (keys == null)
{
return getDefaultString();
}
for (int i = 0; i < keys.length; ++i)
{
if (keys[i] == key)
{
final String[] values = getStringVals();
return values[i];
}
}
return getDefaultString();
}
}

View File

@@ -929,4 +929,7 @@ public interface RSClient extends RSGameEngine, Client
@Import("spellSelected")
@Override
void setSpellSelected(boolean selected);
@Import("getEnum")
RSEnum getRsEnum(int id);
}

View File

@@ -0,0 +1,48 @@
/*
* Copyright (c) 2019, Adam <Adam@sigterm.info>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.runelite.rs.api;
import net.runelite.api.EnumComposition;
import net.runelite.mapping.Import;
public interface RSEnum extends EnumComposition, RSCacheableNode
{
@Import("keys")
int[] getKeys();
@Import("intVals")
@Override
int[] getIntVals();
@Import("stringVals")
@Override
String[] getStringVals();
@Import("defaultInt")
int getDefaultInt();
@Import("defaultString")
String getDefaultString();
}