Add monix-nio and make modding sys use it

This commit is contained in:
Rohan Sircar 2021-03-24 14:21:46 +05:30
parent 8f3c08f271
commit 632bcccef3
3 changed files with 69 additions and 30 deletions

41
build.sbt Normal file → Executable file
View File

@ -42,6 +42,7 @@ lazy val root = (project in file(".")).settings(
"org.typelevel" %% "cats-effect" % "2.3.0",
"io.monix" %% "monix" % "3.2.2",
"io.monix" %% "monix-bio" % "1.1.0",
"io.monix" %% "monix-nio" % "0.0.9",
"io.circe" %% "circe-core" % "0.13.0",
"io.circe" %% "circe-generic" % "0.13.0",
"com.softwaremill.sttp.client3" %% "core" % "3.0.0",
@ -143,7 +144,7 @@ inThisBuild(
List(
scalaVersion := scalaVersion.value, // 2.11.12, or 2.13.3
semanticdbEnabled := true, // enable SemanticDB
semanticdbVersion := "4.3.24" // use Scalafix compatible version
semanticdbVersion := "4.4.10" // use Scalafix compatible version
)
)
ThisBuild / scalafixDependencies += "com.github.liancheng" %% "organize-imports" % "0.4.3"
@ -157,23 +158,23 @@ scalafixDependencies in ThisBuild += "org.scalalint" %% "rules" % "0.1.4"
// wartremoverWarnings in (Compile, compile) ++= Seq(Wart.Any, Wart.Serializable)
wartremoverErrors in (Compile, compile) ++=
Warts.allBut(
Wart.Any,
Wart.NonUnitStatements,
// Wart.StringPlusAny,
Wart.Overloading,
Wart.PublicInference,
Wart.Nothing,
Wart.Var,
Wart.DefaultArguments,
// Wart.MutableDataStructures,
Wart.ImplicitConversion,
Wart.ImplicitParameter,
Wart.ToString,
Wart.Recursion,
Wart.While,
Wart.ExplicitImplicitTypes,
Wart.ListUnapply
)
// wartremoverErrors in (Compile, compile) ++=
// Warts.allBut(
// Wart.Any,
// Wart.NonUnitStatements,
// // Wart.StringPlusAny,
// Wart.Overloading,
// Wart.PublicInference,
// Wart.Nothing,
// Wart.Var,
// Wart.DefaultArguments,
// // Wart.MutableDataStructures,
// Wart.ImplicitConversion,
// Wart.ImplicitParameter,
// Wart.ToString,
// Wart.Recursion,
// Wart.While,
// Wart.ExplicitImplicitTypes,
// Wart.ListUnapply
// )
// Seq(Wart.FinalCaseClass)

View File

@ -16,9 +16,8 @@ import monix.bio.IO
import monix.bio.UIO
import monix.reactive.Consumer
import monix.reactive.Observable
import wow.doge.mygame.utils.IOUtils
import IOUtils.toIO
import wow.doge.mygame.implicits._
import wow.doge.mygame.utils.readAsyncL
@JsonCodec
final case class Test1(hello1: String, hello2: String)
@ -43,13 +42,20 @@ object ModdingSystem {
implicit val eq = Eq.fromUniversalEquals[Error]
}
def readPluginsList2(dir: os.Path) =
for {
contents <- readAsyncL(dir)
} yield ()
def readPluginsList(dir: os.Path): IO[Error, ArraySeq[Plugin]] =
IO(os.read(dir / "plugins.json"))
// IO(os.read(dir / "plugins.json"))
readAsyncL(dir / "plugins.json")
.onErrorHandleWith {
case _: FileNotFoundException =>
IO.raiseError(FileNotFound(dir / "plugins.json"))
case _: NoSuchFileException =>
IO.raiseError(FileNotFound(dir / "plugins.json"))
case other => IO.terminate(other)
}
.flatMap(files =>
IO.fromEither(parse(files))
@ -197,12 +203,12 @@ object ModdingSystem {
UIO(readSuccesses.to(List)),
UIO(parseFailures.to(List)),
UIO(parseSuccesses.to(List)),
toIO(
Observable
.fromIterable(parseSuccesses)
.map { case (p, json) => json }
.consumeWith(loadBalancedPluginDataMerger)
).hideErrors
.toIO
.hideErrors
)(Result.apply)
} yield res

32
src/main/scala/wow/doge/mygame/utils/package.scala Normal file → Executable file
View File

@ -1,6 +1,38 @@
package wow.doge.mygame
import java.nio.file.StandardOpenOption
import monix.bio.UIO
import monix.execution.Scheduler
import monix.nio.file.AsyncFileChannelConsumer
import monix.nio.text.UTF8Codec.utf8Decode
import monix.nio.{file => mnf}
import wow.doge.mygame.implicits._
package object utils {
def methodName(implicit enclosing: sourcecode.Enclosing) =
enclosing.value.split(" ")(0).split("""\.""").last
def readAsync(
path: os.Path,
chunkSize: Int = 8192
) =
UIO
.deferAction(implicit s => UIO(mnf.readAsync(path.toNIO, chunkSize)))
.flatMap(bytes => UIO(bytes.pipeThrough(utf8Decode)))
@SuppressWarnings(Array("org.wartremover.warts.TraversableOps"))
def readAsyncL(
path: os.Path,
chunkSize: Int = 8192
) =
readAsync(path, chunkSize)
.flatMap(_.toListL.toIO)
.map(_.head)
def writeAsync(path: os.Path, flags: Seq[StandardOpenOption] = Seq.empty)(
implicit s: Scheduler
): AsyncFileChannelConsumer =
mnf.appendAsync(path.toNIO, 0, flags)
}